%:: gives the stash of package variables, lexical ('my') variables are held on a PAD. They can be listed using B::Showlex, which is a base module. You can call it at runtime like this:
use strict; use warnings; use B::Showlex; sub dumpit { B::Showlex::compile(-newlex,-nosp,'mysub')->() } sub mysub { my $subvar = 42; my @subvar = qw(The quick brown fox); my %subvar = qw(red live blue neutral green earth); return; } dumpit();
Which gives:
main::mysub: main::mysub Pad has 17 entries 1: PVNV (0x1b49b24) "$subvar" = NULL (0x1b49b44) 2: PVNV (0x1b49ab4) "@subvar" = AV (0x1b49a74) undef 4: PVNV (0x1b49a44) "%subvar" = HV (0x1b49a24) HASH
Update: B::Showlex writes to STDOUT, you can redirect it to a string then extract the variable names:
sub dumpit { my $out; # Redirect STDOUT to a string open my $oldout, ">&STDOUT" or warn "Can't dup STDOUT: $!"; close STDOUT; open(STDOUT,'>',\$out) or warn "STDOUT redirection failed:$!"; B::Showlex::compile(-newlex,-nosp,'mysub')->(); my @lines = split(/\n/,$out); shift @lines; shift @lines; for my $line (@lines) { $line =~ s/.*\"(.\w+)\".*/$1/; print STDERR "$line\n"; } # Restore STDOUT open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!"; }
Update to update: saved and restored STDOUT.

In reply to Re: Dumping the content of a module by cdarke
in thread Dumping the content of a module by jmo

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.