You're close, but you have to dereference the symbol table entry appropriately, not use it directly as a local variable. The array/hash doesn't get magically created:

use strict; use warnings; use feature 'say'; for my $symname (sort keys %main::) { local *sym = $main::{$symname}; say "\@$symname array is populated" if @{ *sym }; # <-- HERE say "\%$symname hash is populated" if %{ *sym }; # <-- and HERE }

Output:

%CORE:: hash is populated %Carp:: hash is populated %DB:: hash is populated %DynaLoader:: hash is populated %ENV hash is populated @INC array is populated %INC hash is populated %IO:: hash is populated %Internals:: hash is populated %PerlIO:: hash is populated %Regexp:: hash is populated %Tie:: hash is populated %UNIVERSAL:: hash is populated %builtin:: hash is populated %constant:: hash is populated %feature:: hash is populated %main:: hash is populated %mro:: hash is populated %re:: hash is populated %strict:: hash is populated %utf8:: hash is populated %version:: hash is populated %warnings:: hash is populated

Here's a bit of an example using the actual entries:

use strict; use warnings; use feature 'say'; for my $symname (sort keys %main::) { local *sym = $main::{$symname}; # Make a copy of the symtab entry if (@{ *sym }) { my @array = @{ *sym }; print "$_\n" for @array; } # Use the symtab entry directly if (%{ *sym }) { for my $key (keys %{ *sym }) { print "$key: ${ *sym }{$key}\n"; } } }

Disclaimer: Please note that using the symbol table directly can be extremely dangerous. You can cause slight or even major problems in very far away code that can be a nightmare or even impossible to track down. This is a very relevant read. It doesn't go into symbol table use directly, but the premise is exactly the same. I find all three parts to be an extremely entertaining and funny read, and it's always nice every couple of years when I have to reference it, as I re-read it each time.

--stevieb


In reply to Re: Interrogating stashes - Camel example doesn't work? by stevieb
in thread Interrogating stashes - Camel example doesn't work? by Intrepid

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.