Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 (Monsignor) 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 (Bishop) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-09-25 07:02 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (277 votes). Check out past polls.