This is IMHO a beautiful story of a clueless question... so clueless that I felt compelled to coin a term like "XWTFITL Problem" to describe it, later spawning an interesting discussion which in turn eventually caused something to end up in a Synopsis, specifically S12.
If you follow the first link at PerlMonks above, you will find that the user asked:
(I include this for completeness, but it may be skipped)
hi,
What I want to do is to store a LoL-path (HoH,AoA,HoA..) and then use it to access the real thing.
Let me give you one example (pseudo code):%map = ( typeA => {key1}[3]{key2}, typeB => {key3}, typeC => [1]{key4}, )and then depending on the type I want to access different element of the LoL. i.e. again pseudo code
$lol->{$map{typeA}} <==> $lol->{key1}[3]{key2} $lol->{$map{typeB}} <==> $lol->{key3} $lol->{$map{typeB}} <==> $lol->[1]{key4}do u have some idea how can i do something like this.
If you didnt understood my question fully, tell me I will try to 'invent' ;) more examples
Now, can you make sense of this question? Most of us couldn't at first, though we guessed it may have to do with easy access to complex data structures and so we suggested modules like Data::Diver, Data::Walker, Data::Dref and Data::Match.
On a second thought some of use including myself realized that the OP wanted to know if there were the possibility of having what he called a "LoL-path" and I a "dereferencing chain" (none complained, so I suppose it was clear enough) as a standalone entity. Of course the poster was concerned with pragmatics, that is, with saving some typing and hopefully have more terse code to work with. In this sense both the above mentioned modules and some other provided solutions must have constituted a mostly definitive answer.
However, notwithstanding the fact that cheap and solid enough solutions exist I'm much concerned with programming languages concepts a.k.a. "philosophy" so I wondered about the "Real™ Thing", of course not in the context of Perl 5 but in Perl 6's: this is the spawned new thread.
To put it briefly, also in the new thread most answers were along the lines of: just use a closure! This, both for 5 and 6, with the minor difference that the latter's syntax was sexier. Thus somewhat they bypassed my "philosophical" question. (There was also an entry additionally mentioning composition with a role.) Indeed $Larry put the final word to it: I underlined that what I wanted to know if there was any provision for "the tail of the animal without the animal existing as a beast itself" and his reply was the one pasted hereafter.
We made it really easy to write closures and deref them in Perl 6 so that people would not feel the need to invent special syntax for deferred evaluation. So basically, you use curlies to delimit any kind of "tail without the animal", and calling a closure is how you add the animal back in. And closures have the advantage of taking additional arguments if you want multiple degrees of "animal" freedom in your locator. S12 is now clarified to indicate that $object.$methodname()
is able to call into a closure as well as into a named method. Thanks.
To paraphrase with my own words: you can have the tail of the animal wrapped up in an extremely thin pellicle.
What's instructive of this, apart the pleasure of having indirectly helped with a tiny contribution, is that what $Larry himself wrote in a previous PerlMonks thread is once again confirmed: namely that Perl's Philosophy, for any revision number, is centered around Pragmatics or that, in some sense, it's Pragmatics that matters and not Philosophy. To quote from that post:
Seriously, what makes Perl Perl? If someone decides I'm ugly and beats my face to a pulp, and later I get a face transplant, am I still me? If my bones rot and are replaced with synthetics, at what point am I a different person? Syntax is just skin. Semantics are just bones. Neither is the soul of Perl, which rests in the realm of pragmatics.
(The emphasis was added by me.)
To finish, as I did in PM, I'm reporting hereafter too the relevant part of the S12:
The variable may contain either the name of a method or a closure object. In the latter case the closure is called with the object as its first argument, so that a closure may be used to abstract a "navigational" path through a data structure without specifying the root of the path till later.
$locator = -> $root, $x, $y { $root.[$x] {$y}[3] } $obj.$locator(42,"baz") # $obj [42] [3] $locator = { . } $obj.$locator # $obj