print variable name not by hash but ref?
am 01.04.2008 10:05:19 von 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?
Re: print variable name not by hash but ref?
am 01.04.2008 14:38:41 von 1usa
"Ela" wrote in
news:fssqc1$3f7$1@ijustice.itsc.cuhk.edu.hk:
> 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.
The closest thing I know of is
http://search.cpan.org/~robin/PadWalker-1.7/PadWalker.pm
Read the docs, especially the warnings.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
Re: print variable name not by hash but ref?
am 01.04.2008 14:40:20 von smallpond
On Apr 1, 4:05 am, "Ela" wrote:
> 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'