Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

how to print out a variable name and its value

by perlfan99 (Sexton)
on Jul 30, 2008 at 06:36 UTC ( #701033=perlquestion: print w/ replies, xml ) Need Help??
perlfan99 has asked for the wisdom of the Perl Monks concerning the following question:

is there a way to pass a variable to a function, which will output the variable name and its value, something like this:

sub print_variable { .... print variable . "=" . $variable } $firstname = "Edward"; $var = 1; print_variable( $firstname ); print_variable( $var ); output: firstname = Edward var = 1


Thanks!

Comment on how to print out a variable name and its value
Download Code
Re: how to print out a variable name and its value
by Corion (Pope) on Jul 30, 2008 at 06:43 UTC

    Yes - use Data::Dumper::Names:

    use Data::Dumper::Names; my $foo = 3; my @bar = qw/this that/; warn Dumper($foo, \@bar); __END__ output: $foo = 3; @bar = ( 'this', 'that' );
      thanks! i guess if i want to format the output differently, i need to read Dumper source code.

        Would

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

        -- 
        Ronald Fischer <ynnor@mm.st>
        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 (κς,πμ,πλ)
Re: how to print out a variable name and its value
by shmem (Canon) on Jul 30, 2008 at 08:06 UTC

    hackish solution:

    sub print_variable { local *print_variable = $_[0]; my $name = $print_variable; for (keys %::){ next if /^print_variable$/; print "$_ = $name\n" if *{$::{$_}}{SCALAR} eq *{$::{print_variable}}{SCALAR}; } } $firstname = "Edward"; $var = 1; print_variable( *firstname ); print_variable( *var ); __END__ firstname = Edward var = 1

    Doesn't work with my variables, only with symbol table entries. Also, if you alias a typeglob, you get them all:

    $firstname = "Edward"; *surname = *firstname; print_variable( *firstname ); __END__ firstname = Edward surname = Edward

    update: fixed b0rken code...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: how to print out a variable name and its value
by Bloodnok (Vicar) on Jul 30, 2008 at 10:58 UTC
    Is the 'Passing by Named Parameter' recipe (Recipe 10.7), from the Perl Cookbook of no use ?

    e.g.

    use data::Dumper; sub some_sub { my $args = { first_name => '', surname => '', @_ }; print Dumper $args; } some_sub qw/Edward Lear/;
    will return something along the lines of...
    $VAR1 = { first_name => 'Edward', surname => 'Lear', };

    It's what I tend to use ...

    Update Thanx to massa who pointed out the obvious error in my example code:

    some_sub qw/Edward Lear/;
    doesn't call the sub with named parameters, had it read:
    some_sub first_name => 'Edward', surname => 'Lear';
    it would.
    Thanx again massa.

    HTH ,

    At last, a user level that overstates my experience :-))
      Your example is expecting named parameters, but it's not sending the named parameters:
      use Data::Dumper; sub some_sub { my $args = { first_name => '', surname => '', @_ }; print Dumper $args; } some_sub first_name => 'Edward', surname => 'Lear';
      will work!
      []s, HTH, Massa (κς,πμ,πλ)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://701033]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-09-02 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls