http://qs321.pair.com?node_id=617609


in reply to storing and using LoL path

If I'm understanding you correctly (which I'm not at all sure), what you want is something like could be achieved with the following if/elsif... structure

sub lookup { my ($type, $ref) = @_; if ($type eq 'typeA') { return $ref->{key1}[3]{key2}; } elsif ($type eq 'typeB') { return $ref->{key3}; } elsif ($type eq 'typeC') { return $ref->[1]{key4}; } # ... }

i.e. with the following minimal sample data

my @LoANY = ( { type => "typeA", data => { key1 => [0,1,2, { key2 => "Value1" } ] } }, { type => "typeB", data => { key3 => "Value2" } }, { type => "typeC", data => [0, { key4 => " Value3" } ] }, # ... );

this loop

for my $elem (@LoANY) { print lookup($elem->{type}, $elem->{data}), "\n"; }

would print

Value1 Value2 Value3

But you think the if/elsif thingy is not perlish enough, would not scale decently up to a gazillion of different types, or some such... (?)

In that case, one other way to do it would be to set up little "accessor" functions (not in the OO sense, thus the quotes), which you would index via the hash, e.g.

my %map = ( typeA => sub { $_[0]->{key1}[3]{key2} }, typeB => sub { $_[0]->{key3} }, typeC => sub { $_[0]->[1]{key4} }, # ... );

In that case, you could write the above loop as

for my $elem (@LoANY) { print $map{$elem->{type}}->($elem->{data}), "\n"; }

The type would select the appropriate function (via %map), which "knows" how to get at the desired data. The data (i.e.the toplevel ref to some data structure) is passed to the function as argument.

I'm sure that once you confirm this is what you want to do, other Monks will come up with various other solutions... :)

Replies are listed 'Best First'.
Re^2: storing and using LoL path
by rootcho (Pilgrim) on May 25, 2007 at 23:51 UTC
    Sorry all, for not being too descriptive.. 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} }, # ... );
    i.e. in my concentrate case I was looping over a result of SOAP::Lite. Which depending on the type of the object places the 'price' in different places f.e.
    $$obj{cost} <--type1 $$obj{priceList}{price} <--type2 $$obj{priceList}{price}{value} <--type3 ...etc..
    So instead of doing alot of if-typeX, I thought it will be good if I can store the access-path in some way and do a simple hash lookup.
    Is it more clear now ;)
    (Something like XPath on LoL)
      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)