in reply to Interrogating stashes - Camel example doesn't work?
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Interrogating stashes - Camel example doesn't work?
by jdporter (Paladin) on Jul 24, 2025 at 20:40 UTC | |
Re^2: Interrogating stashes - Camel example doesn't work?
by Intrepid (Curate) on Jul 28, 2025 at 00:44 UTC | |
by hossman (Prior) on Jul 29, 2025 at 22:57 UTC | |
by hippo (Archbishop) on Jul 28, 2025 at 09:55 UTC |