Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Can you find the name of a variable?

by ikegami (Pope)
on Nov 13, 2009 at 19:18 UTC ( #807031=note: print w/ replies, xml ) Need Help??


in reply to Can you find the name of a variable?

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


Comment on Re: Can you find the name of a variable?
Download Code
Re^2: Can you find the name of a variable?
by JadeNB (Chaplain) on Nov 15, 2009 at 07:51 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-07-10 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls