September 2017
Beginner
402 pages
9h 52m
English
We continue with the House class and now it is time to think about the values that can be stored in the attributes.
In the previous examples, we set them to some meaningful values, but the language does not resist if you put some nonsensible data; for example, if we accidentally put an address to the $.area attribute:
my $house = House.new( rooms => 2, area => 'Calle Velázquez 57, 28001 Madrid, Spain', height => 4.0, address => 100.0,);
Imagine now that you want to calculate the average area of one room:
say $house.area / $house.rooms;
This cannot be done, as the $.area attribute contains a string, not a number. The compiler reports a runtime error:
Cannot convert string to number: base-10 number must begin with valid digits ...
Read now
Unlock full access