The other day I was playing around with a two little OO modules, I'll call one Item and one Collection for sake of argument.
Item has two main attributes:
VAL :a scalar value. and
USABLE :a flag that denotes the items availability.
Collection is just that: a collection of Item objects with added methods to do stuff with the collections. It's main attribute is ITEMS which is a referance to an array of Item objects.
Now that I've set the scene and bored half the audience to tears I come to the problem that led to enlightenment.
I wanted a method in Collection to return a scalar that was a concatenation of the values (sorted numerically) of all the objects in the collection whose USABLE flag was true. I ended up with this:
#method in Collection object stringify { my $self = shift; return join("",sort {$a <=> $b} map {$_->{VAL}} grep {$_->{USABLE}} @$self->{ITEMS}); }
The great part is, once I wrote it, it seemed 'natural'. It just fit. I used to read this kind of code and go "uhhhh, Ok that's cool but I'll never write something like that let alone be able to decipher it". I feel I've crossed some sort of learning plateau, or passed an important rite of passage.
That's it.. on to the next level
-ase