http://www.perlmonks.org?node_id=1034688
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

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