Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

eval point in hash ref

by ag4ve (Monk)
on May 22, 2013 at 06:50 UTC ( #1034688=perlquestion: print w/replies, xml ) Need Help??
ag4ve has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to be able to do:
./dat_query '{seek}'
./dat_query '{seek}{messages}'

and have code do:
print Dumper($nethist->{seek});
print Dumper($nethist->{seek}{messages});

I've been trying:
my $in = $ARGV[0]; my $val = eval '$nethist->$in'; print Dumper($val);
and the like with no luck

Replies are listed 'Best First'.
Re: eval point in hash ref
by hdb (Prior) on May 22, 2013 at 07:20 UTC

    Is this what you want?

    use strict; use warnings; use Data::Dumper; my $nethist; $nethist->{seek} = 42; my $in = '{seek}'; my $val = eval '$nethist->'.$in; print Dumper($val);
      Thanks, that's what I was looking for.
Re: eval point in hash ref
by LanX (Chancellor) on May 22, 2013 at 10:10 UTC
    to eval commandline arguments is always prone to code injection

    you might wanna use a simple recursion:

    DB<119> sub dive { my $val=shift; $val = $val->{$_} for @_; return $val; } DB<120> $nethist => { "seek" => { messages => 42 } } DB<121> dive ($nethist) => { "seek" => { messages => 42 } } DB<122> dive ($nethist,"seek","messages") => 42 DB<123> dive ($nethist,"seek") => { messages => 42 }

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: eval point in hash ref (Data::Diver)
by Anonymous Monk on May 22, 2013 at 07:13 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1034688]
Approved by ww
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2016-09-26 13:28 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (478 votes). Check out past polls.