Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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
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 (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)

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

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 making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2015-07-30 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls