September 2017
Beginner
402 pages
9h 52m
English
Assume now that we are building a street and would like to give house numbers to any new object. For simplicity, let us temporarily remove all the attributes except the $.address in House and $.housenumber in Address:
class Address { has Int $.housenumber is rw;}class House { has Address $.address is rw;}
The next step is a loop that builds houses and saves them in the @street array:
my @street;for 1..10 { push @street, House.new( address => Address.new( housenumber => @street.elems + 1 ) )}
To increment the house number, we use the size of the @street in the following way:
@street.elems + 1
This ensures that every new house gets a number, which is greater than the number of all existing houses. To see that, loop over the ...
Read now
Unlock full access