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
    Sometimes I'm one of the people who says to use a hash instead, and sometimes I'm one of the people who answers the question that was asked, even though I think they should be using a hash instead.

    Classic MJD! 😄 (see XY Problem for more examples)

Re^2: Interrogating stashes - Camel example doesn't work?
by Intrepid (Curate) on Jul 28, 2025 at 00:44 UTC

    stevieb began his expository goodness with:

    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

    I see! I wonder if something has changed in how Perl parses such things, since, as I mentioned, the example was virtually verbatim from The Divine Book of All Things Perlish (the "Camel"). Whatever the case, your assistance caused me to become enlightened and was very appreciated. I've incorporated this dereferencing technique in code in a module I'm working on.

        - Soren

    Jul 28, 2025 at 00:42 UTC

      You are getting different results, because you are using strict & warnings -- neither of which are enabled in the original sample code in the book....

      foreach $symname (sort keys %main::) { local *sym = $main::{$symname}; print "\$$symname is defined\n" if defined $sym; print "\@$symname is nonnull\n" if @sym; print "\%$symname is nonnull\n" if %sym; }
      the example was virtually verbatim from [the Camel Book]

      Perhaps it would be a good exercise to try it actually verbatim? That way you will be enlightened as to whether something has indeed changed or if it was just your tweaks which have caused the problem.


      🦛