SPUG: debugging perl

tom Coleman tom_g_coleman at hotmail.com
Wed Jan 28 17:57:05 CST 2004


hi Michael,

>>you asked:
Is there a way to find the current identifier name? That is, given $x
or @y (or \$x or \@y), can you get back "$x" or "@y"?

>>here's a suggestion:

Passing the variables as references is a step in the right direction.  You 
could bless them with the name you wish to identify by... then extract the 
blessed name to get the variable name.

@stuff_to_debug = ( bless(\$x, "scalar_x", bless(\$y, "scalar_y", bless(\$z, 
"scalar_z") );

foreach (@stuff_to_debug) {
    printf "%s %d\n", $$_, <criptic thing here>;
}
I have not taken the time to extract the blessed name from the reference to 
get the variable name.  A more complex method could deal with more than just 
scalars.

#####
a more direct solution... Perl supports conversion between strings to 
variables...  just pass the variables as strings.

@stuff_to_debug = ( "$x", "$y", "$z" );

foreach (@stuff_to_debug) {
    printf "%s %d\n", $_, <criptic thing here>;
}

I have not taken the time to convert from string to variable to get value... 
something like:

my $var_string = "x";
my $value = ${$var_string};

good luck!
tom coleman

_________________________________________________________________
Scope out the new MSN Plus Internet Software — optimizes dial-up to the max! 
   http://join.msn.com/?pgmarket=en-us&page=byoa/plus&ST=1




More information about the spug-list mailing list