in reply to Re^2: Dotted hash access
in thread Dotted hash access
That method looks at the big data structure, pulls out the reference which is the value for the key $l (which has no restrictions on its characters), blesses it as the mini-class Location, and returns it. It's the same old reference, there is no copy, but you can call methods on it. Not only that, it's easy to see what it's doing because there isn't a lot of things happening at that level.$location = $thingy->get_location( $l );
That's the same thing, called on the previous mini-object. All the data is still in the big data structure, but you have this hook into it because the reference $building is blessed into the Buidling class. Again, it's the same old reference that's in the data strucutre, but it now knows how to respond to method calls. Or, you can do this in one step.$building = $location->get_building( $b );
Or$building = $thingy->get_location( $l )->get_building( $b ); #or $building = $thingy->get_building_by_location( $l, $b );
If you want to iterate through everything:$cost = $thingy->get_location( $l )->get_building( $b )->cost; $totals = $location->get_totals; $upkeep = $building->get_upkeep;
You can define all sorts of other iterators, visitors, and cool things. You don't have to know anything about the data structure. You have a lot of flexibility with this approach, and it uses vanilla Perl syntax that people can read about it in books and in the documentation. You don't have to create any new way to do thing, which means you don't have to create new logic or new bugs. If you want iterators, you can create those yourself inside the class. I talk about all sorts of these things in The Perl Review 0.5.foreach my $l ( $thingy->all_locations ) { foreacn my $b ( $b->all_buildings ) { $b->set_cost( $b->cost + 1 ); } }
So don't create a new dialect, which just adds to the complexity of your code. You should code not so you understand it, but so other people will understand. :)while( my $location = $thingy->next_location ) { ... }
|
|---|