jr has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'd like to use Data::Dumper to dump something which I type in <STDIN>. I tried to enter test first one and used something like for ( split " ", $input ) { $dump .= "->{$_}"; } but that didn't seem to work. I also tried entering $test->{first}->{one} and dumping ${$input} but that didn't seem to work either.

Any ideas on how to go about doing this?

Thanks

use strict; use warnings; our $test = { first => { one => { number => 1 }, two => { number => 2 }, three => { number => 3 } }, second => { one => { number => 1 }, two => { number => 2 }, three => { number => 3 } }, third => { one => { number => 1 }, two => { number => 2 }, three => { number => 3 } } }; my $input = <STDIN>; $input =~ s/\n$//; no strict 'refs'; my $dump = ${$input}; use strict 'refs'; use Data::Dumper; print Dumper( $dump ), "\n";

Replies are listed 'Best First'.
Re: Getting Data::Dumper target from STDIN
by toolic (Bishop) on Jan 27, 2015 at 15:34 UTC
    This dumps something for me:
    use strict; use warnings; our $test = { first => { one => { number => 1 }, two => { number => 2 }, three => { number => 3 } }, second => { one => { number => 1 }, two => { number => 2 }, three => { number => 3 } }, third => { one => { number => 1 }, two => { number => 2 }, three => { number => 3 } } }; my $input = <STDIN>; $input =~ s/\n$//; my $dump = $test->{$input}; use Data::Dumper; print Dumper( $dump ), "\n"; __END__ % myprog.pl first $VAR1 = { 'three' => { 'number' => 3 }, 'one' => { 'number' => 1 }, 'two' => { 'number' => 2 } };

    see also:

      That worked too but I was looking for something that would dump $a->{$b}->{$c}... rather than just $a->{$b}.

      Thanks for your answer!

Re: Getting Data::Dumper target from STDIN
by Corion (Patriarch) on Jan 27, 2015 at 15:33 UTC

    I would use something like Data::Diver to get the reference.

    As an alternative approach, if you're willing to use Perl syntax, you could get the reference using eval, with the problem that you can also call functions etc. from eval:

    my $dump= eval $input; if( my $err= $@ ) { warn $err; } else { print Dumper $dump; };

      This worked perfectly.

      Thanks!

Re: Getting Data::Dumper target from STDIN
by ww (Archbishop) on Jan 27, 2015 at 16:48 UTC

    One alternate (using Dumper omitted, because how to do so is not the root of OP's problem):

    >perl -E "while (<STDIN>) {say shift; push @input, $_;} say @input;" foo bar baz bat sit sat sot ^Z foo bar baz bat sit sat sot

    Another way, with slight (and adaptable) variation on OP's spec:

    C:>perl -E "use Data::Dumper; say Dumper @ARGV;" foo bar baz bat $VAR1 = 'foo'; $VAR2 = 'bar'; $VAR3 = 'baz'; $VAR4 = 'bat';

Re: Getting Data::Dumper target from STDIN
by GotToBTru (Prior) on Jan 27, 2015 at 15:36 UTC

    Nevermind - misread the question!

    Interpolation is messing up your input. Print out the value of your variable to see what it is. Also, I would use shift instead of STDIN to get the values included on the command line.

    $input = shift; print "$input\n";
    Dum Spiro Spero