Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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}'
or
./dat_query '{seek}{messages}'

and have code do:
print Dumper($nethist->{seek});
or
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

Comment on eval point in hash ref
Select or Download Code
Re: eval point in hash ref (Data::Diver)
by Anonymous Monk on May 22, 2013 at 07:13 UTC
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 (Canon) 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)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-11-24 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (141 votes), past polls