The closest I want to accomplish is the this example u shown :
my %map = ( typeA => sub { $_[0]->{key1}[3]{key2} }, typeB => sub { $_[0]->{key3} }, typeC => sub { $_[0]->[1]{key4} }, # ... );

In what way does this not do what you described?

Well, another way would be to store the "XPath" as a string in the hash, and then eval it at runtime, i.e.

my %map = ( typeA => "{key1}[3]{key2}", typeB => "{key3}", typeC => "[1]{key4}", # ... ); for my $elem (@LoANY) { my $xpath = $map{$elem->{type}}; my $lol = $elem->{data}; print eval "\$lol->$xpath", "\n"; }

With the example data from my previous post, this would also print

Value1 Value2 Value3

In other words, you construct snippets of literal Perl source (e.g. "\$lol->$xpath", interpolating to '$lol->{key1}[3]{key2}'), which you then eval.  Thing is that the XPath thing would need to be turned into runnable code — either at compile-time (like in the function dispatcher approach shown first), or with an eval at runtime.

Expect a performance penalty though, if you call the eval many many times... (Update: not to mention that using eval would require some awareness of security issues, if you apply it in scenarios where the code being constructed dynamically is potentially coming from insecure sources)


In reply to Re^3: storing and using LoL path by almut
in thread storing and using LoL path by rootcho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.