Something silly...
You can use tie to follow your variable use during the run of your program.
Keep in mind that you can't directly access the name of the variable yourself, you have to pass the name as a parameter to your bound object.
The code would go like this:
package Defined; use strict; use warnings; sub TIESCALAR { my $class = shift; my $name = shift; my $self = { 'NAME' => $name, 'DEFINED' => undef, }; return bless ($self, $class); } sub FETCH { my $self = shift; my $name = $self->{'NAME'}; my @caller = caller; if (defined $self->{'DEFINED'}) { print STDERR "@caller -> $name defined\n"; } else { print STDERR "@caller -> $name undefined\n"; } } sub STORE { my $self = shift; my $value = shift; my $name = $self->{'NAME'}; my @caller = caller; $self->{'DEFINED'} = $value; print STDERR "@caller -> $name defined with value $value\n"; } 1;
#!/usr/bin/perl use Defined; use strict; use warnings; my $var; tie $var, 'Defined', '$var'; print $var; $var = 1; print $var; $var = 5
bash-3.00$ ./test.pl main ./test.pl 11 -> $var undefined main ./test.pl 12 -> $var defined with value 1 main ./test.pl 13 -> $var defined main ./test.pl 14 -> $var defined with value 5
As I said, a bit silly. However, it might helpt in some cases of debugging.
In reply to Re: Access variable names
by gargle
in thread Access variable names
by andreas1234567
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |