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 (Defined.PM)
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;
The caller (test.pl)
#!/usr/bin/perl use Defined; use strict; use warnings; my $var; tie $var, 'Defined', '$var'; print $var; $var = 1; print $var; $var = 5
Output
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.

--
if ( 1 ) { $postman->ring() for (1..2); }

In reply to Re: Access variable names by gargle
in thread Access variable names by andreas1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.