The fact that you want to do this means that you are either writing a debugger, or you are looking at the problem from the wrong angle (e.g. "it's stupid to use a variable as a variable name", and you should use a hash instead). If you could tell us why you want to do this, we can almost certainly provide better solutions.

PadWalker will provide you with enough rope to shoot yourself in the foot:

use warnings; use strict; use PadWalker qw/peek_my/; use Data::Dump; my $A; my $B = 0; dd peek_my(0); for my $i ( $B .. 3 ){ my $C = 3; dd peek_my(0); } __END__ { "\$A" => \undef, "\$B" => \0 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \0 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \1 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \2 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \3 }

Update: That only dumps my variables. See also the module's peek_our function, or for inspecting the Symbol Tables of packages, Devel::Symdump or my simplistic replacement here (only does scalars, arrays and hashes). When dumping symbol tables, note the caveat described in the Devel::Symdump documentation: "scalar symbol table entries are a special case. If a symbol table entry exists at all, presence of a scalar is currently unknowable, due to a feature of Perl described in "Making References" in perlref point 7. For example, this package will mark a scalar value $foo as present if any of @foo, %foo, &foo etc. have been declared or used."


In reply to Re: Is it possible to get all variable names which are declared? (updated) by haukex
in thread Is it possible to get all variable names which are declared? by rsFalse

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.