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

Re: Displaying a variable's name and value in a sub

by Marshall (Canon)
on Feb 15, 2011 at 09:42 UTC ( [id://888175]=note: print w/replies, xml ) Need Help??


in reply to Displaying a variable's name and value in a sub

I find the idea, "This is trivial in C/C++ using macros" hard to believe. I can't see any trivial way without expanding the number of args to showx().

Of course you can use the C pre-possessor to do macro expansion and send that result into Perl if you wanted to do so. The C pre-processor does not know any thing about the C language. I would be curious as what you come up with. But I doubt that your original claim is correct.

sub show1 { ... } sub show2 { ... } my $a = 'foo'; show1($a); #desired print $a='foo' show2('$a'); #desired print $a='foo' $a = 'foobar'; show1($a); #desired print $a='foobar'
Perl has position dependent variable passing, just like 'C'. "show1()" should be saying something like "name was $name" where "$name = shift;". For show1() to display a calling variable name, it would have to have an additional parameter, $souce_code_name.

There is not a way to easily get the calling variable name from the sub that called this sub even with C macro expansion.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found