in reply to Re: how to print out a variable name and its value
in thread how to print out a variable name and its value

thanks! i guess if i want to format the output differently, i need to read Dumper source code.
  • Comment on Re^2: how to print out a variable name and its value

Replies are listed 'Best First'.
Re^3: how to print out a variable name and its value
by massa (Hermit) on Jul 30, 2008 at 12:21 UTC
    Dumper can be controlled by some attributes, read the man pages first! I, personally, like Data::Dump::Streamer better:
    $ perl -e ' use Data::Dump::Streamer; my $firstname = "Edward"; my $var = 1; my @jack = ('a', '1', { b => 3, c => [1, 2]}); DumpLex($firstname, $var, \@jack)->Indent(0)->OptSpace(" ")->Out(); ' $firstname = 'Edward'; $var = 1; @jack = ( 'a', 1, { b => 3, c => [ 1, 2 ] } );
    Or, if the vars aren't lexical,
    $ perl -e ' use Data::Dump::Streamer; $firstname = "Edward"; $var = 1; @jack = ('a', '1', { b => 3, c => [1, 2]}); DumpVars(firstname => $firstname, var => $var, jack => \@jack)->Indent +(0)->OptSpace(" ")->Out(); ' $firstname = 'Edward'; $var = 1; $jack = [ 'a', 1, { b => 3, c => [ 1, 2 ] } ];
    []s, HTH, Massa (κς,πμ,πλ)
      Mucho gracias. Else, I would have not been interested in the D'D'Streamer for D'D has been good enough for me.
Re^3: how to print out a variable name and its value
by rovf (Priest) on Jul 30, 2008 at 07:19 UTC

    Would

    # Warning! Code not tested! $_=eval($name); print(defined $_ ? ("$name=[$_]") : "$name is undefined");
    do the trick?

    -- 
    Ronald Fischer <ynnor@mm.st>
      no, it doesn't work. it prints out the variable value not its name

        Hmmmm.... For me it works. Here a complete program to demonstrate the concept:

        use strict; use warnings; my $var1=35; my $var2=undef; foreach my $name ('$var1','$var2') { $_=eval($name); print(defined $_ ? ("$name=[$_]") : "$name is undefined","\n"); }
        This prints:
        $var1=[35] $var2 is undefined

        -- 
        Ronald Fischer <ynnor@mm.st>