Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: passing a variables name to a subroutine

by moritz (Cardinal)
on Feb 18, 2011 at 12:13 UTC ( #888888=note: print w/ replies, xml ) Need Help??


in reply to passing a variables name to a subroutine

for a debugging-sub I would like to pass the name of a variable to the sub. How is this possible?
my $x; yoursub '$x';

Inside your sub you can use PadWalker to access foreign lexical variables, and caller plus symbol table magic for package variables.


Comment on Re: passing a variables name to a subroutine
Download Code
Re^2: passing a variables name to a subroutine
by LANTI (Sexton) on Feb 18, 2011 at 13:55 UTC

    hmh ... seems not to work, if the dumped ref is a lexical variable in production-code. For the moment I live with this solution - not very elegant, because most of the time the identifying string will be same as the var-name:

    say sdump($pout, '$pout'); sub sdump { my $ref = shift || croak('nothing to dump - missing ref'); my $name = shift || 'n/a'; my $depth = shift || 2; local $Data::Dumper::Maxdepth = $depth; return "Dump of $name:\n" . Dumper($ref); }

    Any idea, how to call it this way?

    sdump('$pout');
      hmh ... seems not to work, if the dumped ref is a lexical variable in production-code.

      With which code did it not work? Did you try PadWalker as I suggested? How the the production code differ from the test code?

Re^2: passing a variables name to a subroutine
by jdporter (Canon) on Jul 19, 2012 at 15:40 UTC

    Congrats on winning 888888. :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2015-07-04 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls