http://www.perlmonks.org?node_id=807025

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

Is there a way to find the name of a variable?

For example, I'm checking the args to a function like this:

sub blah { my ($foo,$bar,$baz) = @_; for my $arg (\$foo,\$bar,\$baz) { if ( !defined($$arg) ) { croak "You forgot something!\n"; } } ... }

It would be nice to be able to say "You forgot foo!" if $foo was undefined. I know there are other ways to solve the actual problem, but I'm curious if there is a way to know the name of a variable.

--Thanks!
Pileofrogs

Replies are listed 'Best First'.
Re: Can you find the name of a variable?
by ikegami (Patriarch) on Nov 13, 2009 at 19:18 UTC

    I know there are other ways to solve the actual problem

    Just as simple as your code:

    sub blah { my ($foo, $bar, $baz) = @_; for my $arg (qw( foo bar baz )) { croak("$arg missing/undefined") if !defined(shift); } ... }

    but I'm curious if there is a way to know the name of a variable

    PadWalker

      sub blah { my ($foo, $bar, $baz) = @_; for my $arg (qw( foo bar baz )) { croak("$arg missing/undefined") if !defined(shift); } ... }

      I know that I must be missing something here (UPDATE: yup!), but why not just

      sub blah { my ( $foo, $bar, $baz ); for my $arg ( ( $foo, $bar, $baz ) = @_ ) { croak "You forgot something" unless defined $arg; } do_something; }
      or even something like the List::MoreUtils-flavoured
      sub blah { croak "You forgot something" unless all { defined } ( my ( $foo, $ +bar, $baz ) = @_ ); do_something; }
      That is, why have the for loop run over the argument list ‘indirectly’ when it's right there to be had?

      UPDATE: Indeed, I missed the entirety of the author's point, which was precisely that he wanted more than what his code snippet already did—namely, to detect which, if any, of the relevant variables was already defined.

        I know that I must be missing something here

        They don't produce the right output. The OP wants to identify which arg is not defined by name.

Re: Can you find the name of a variable?
by chromatic (Archbishop) on Nov 13, 2009 at 19:25 UTC

    Untested, but:

    use PadWalker 'var_name'; for my $arg (\($foo, $bar, $baz)) { next if defined $$arg; my $name = var_name(0, $arg); croak "You forgot to define $name!\n"; }
Re: Can you find the name of a variable?
by afoken (Chancellor) on Nov 14, 2009 at 10:57 UTC

    If you want to know the name of a variable, you usually try to do something stupid. Most times, the answer is to use a hash instead. Also in this case, you can use a hash to get named parameters instead of positional parameters.

    Something like this:

    sub blah { my %args=(@_); foreach my $needed (qw( foo bar baz )) { exists $args{$needed} or die "blah() needs a $needed parameter +"; } # .... } blah(foo => 42, bar => "don't forget this", baz => 'oh!'); blah(foo => 42, baz => 'oh!'); # <-- will die()

    Back to the "what's the name of the variable problem". What should a hypothetical nameThatVariable() function return for the following cases?

    $foo=42; print nameThatVariable($foo); # '$foo', of course. $foo=[ 42 ]; print nameThatVariable($foo); # still '$foo'. @bar=( 42 ); print nameThatVariable(@bar); # '@bar', of course. $baz=$foo=\@bar; print nameThatVariable($foo); # 'reference to @bar' or '$foo'? print nameThatVariable($baz); # 'reference to @bar' or '$foo' or '$baz +' ? print nameThatVariable(\@bar); # 'reference to @bar'? print nameThatVariable(do { my $tmp=23 }); # '$tmp'? print nameThatVariable([ 23,45 ]); # ?

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Can you find the name of a variable?
by ambrus (Abbot) on Nov 15, 2009 at 11:07 UTC