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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |