print variable name not by hash but ref?

E

Ela

some day i read a book telling that it may be useful to print a variable
name for debugging purpose. since i was new that moment, i just skipped it.
now when i want to use this feature, i no longer get back where it is.
google search using perl and "print variable name" does not return good
results. And I am not going to use hash because i just want to debug, e..g.
I expect

$var1 = 3;
$ALongVariable = "hahahaha";

debug($var1);
debug($ALongVariable);

sub debug {
$dvar = shift;
some more codes here?
print $dvar;
print "\n";
}

=======
to print out:

$var1 : 3
$ALongVariable : hahahaha

Could anybody help?
 
A

A. Sinan Unur

S

smallpond

some day i read a book telling that it may be useful to print a variable
name for debugging purpose. since i was new that moment, i just skipped it.
now when i want to use this feature, i no longer get back where it is.
google search using perl and "print variable name" does not return good
results. And I am not going to use hash because i just want to debug, e..g.
I expect

$var1 = 3;
$ALongVariable = "hahahaha";

debug($var1);
debug($ALongVariable);

sub debug {
$dvar = shift;
some more codes here?
print $dvar;
print "\n";

}

=======
to print out:

$var1 : 3
$ALongVariable : hahahaha

Could anybody help?


That can't work since only the value is passed to your
debug sub, not the variable.

You can get to package variables through the symbol table
but I don't know how to get to lexicals.

perl -e 'our $foo=5; print join "\n", keys %main::;' |grep foo
foo

perl -e 'my $foo=5; print join "\n", keys %main::;' |grep foo
<== no package variable named 'foo'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top