Click here to get back home

print variable name not by hash but ref?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
print variable name not by hash but ref? Ela 04-01-2008
Posted by Ela on April 1, 2008, 4:05 am
Please log in for more thread options
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?



Posted by A. Sinan Unur on April 1, 2008, 8:38 am
Please log in for more thread options

> 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

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by smallpond on April 1, 2008, 8:40 am
Please log in for more thread options
> 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'



Similar ThreadsPosted
Use of uninitialized variable with print << HTMLEND (and $ENV) October 31, 2006, 5:28 pm
capture output from print command to variable in perl April 15, 2005, 4:32 pm
How to print modified version of string without intermediate variable? November 10, 2005, 2:13 pm
How to print like debuggers `x $HASH` November 22, 2004, 4:55 pm
Print modified version of a string w/o intermediate variable or subroutine October 7, 2005, 2:55 pm
print thought given reference to a hash May 19, 2006, 5:37 pm
OO perl, why print hash keys AND values? June 2, 2008, 10:22 am
Print dereferenced hash of arrays keys and values May 24, 2006, 8:12 pm
print statement creates key in hash with reference to empty array?? January 31, 2006, 11:24 am
comments in the middle of a hash variable ? July 21, 2005, 7:56 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap