in reply to keys %::

I changed it to also print out the values, and am delighted to find:
# on cygwin perl -e 'print "$_ => $::{$_}\n" for keys %::' __OUTPUT__ / => *main::/ stderr => *main::stderr _<.\win32.c => *main::_<.\win32.c _<perllib.c => *main::_<perllib.c utf8:: => *main::utf8:: " => *main::" CORE:: => *main::CORE:: DynaLoader:: => *main::DynaLoader:: stdout => *main::stdout attributes:: => *main::attributes:: &#8597; => *main::&#8597; stdin => *main::stdin ARGV => *main::ARGV INC => *main::INC ENV => *main::ENV Regexp:: => *main::Regexp:: _<..\perlio.c => *main::_<..\perlio.c UNIVERSAL:: => *main::UNIVERSAL:: $ => *main::$ main:: => *main::main:: - => *main::- Win32:: => *main::Win32:: PerlIO:: => *main::PerlIO:: _<..\universal.c => *main::_<..\universal.c 0 => *main::0 => *main:: @ => *main::@ _<..\xsutils.c => *main::_<..\xsutils.c STDOUT => *main::STDOUT IO:: => *main::IO:: &#8593; => *main::&#8593; _ => *main::_ + => *main::+ STDERR => *main::STDERR Internals:: => *main::Internals:: STDIN => *main::STDIN DB:: => *main::DB:: <none>:: => *main::<none>::
This "confirms" what I read somewhere that the symbol table contains the typeglob of the key, which apparently works for any package. Is there anyway to find out what the typeglobs really are? (i.e, its scalar value, array value, etc.)

Replies are listed 'Best First'.
Re^2: keys %::
by starbolin (Hermit) on Apr 29, 2005 at 21:48 UTC
Re^2: keys %::
by Animator (Hermit) on Apr 29, 2005 at 21:12 UTC

    Yes... to see if something is a scalar you can do *something{SCALAR}. if it is a scalar then it will exists and it will be defined... (read `perldoc perlmod` if you need more information)

    Or you could use the Devel::Symdump package...

      I seem to recall that every glob has an SV in the scalar slot as an optimization, but I can't confirm it by reading the source code. (I'm not sure I've even found the right spot in the code.)

        Indeed it is so, for when I first tried I noticed that the 'SCALAR' key always seemed to be there, which surprised me and back then I checked into the docs to discover that it was supposed to be so. I think it hasn't changed since...
Re^2: keys %::
by friedo (Prior) on Apr 30, 2005 at 08:26 UTC
    Is there anyway to find out what the typeglobs really are? (i.e, its scalar value, array value, etc.)

    Each slot in the GLOB, if defined, contains a reference to the actual value. The following works for looking up scalars, arrays, and hashes:

    for my $g(sort keys %::) { print "$g:\n"; if(defined( *{$g}{SCALAR} ) ) { print " SCALAR: ", ${ *{$g}{SCALAR} }, "\n"; } if(defined( *{$g}{ARRAY} ) ) { print " ARRAY: ", join ", ", @{ *{$g}{ARRAY} }, "\n"; } if(defined( *{$g}{HASH} ) ) { print " HASH: "; while(my ($k, $v) = each %{ *{$g}{HASH} } ) { print "$k => $v, " } } print "\n" }
      I have improved this script a bit, it can recurse into symbol tables now.
      I learned something new today. Internals::SvREADONLY can reset the readonly flag :)
      my $table=shift||''; $table.="::"; dumptable($table); sub dumptable{ my $table=shift; my $indent=shift||''; while (my ($key,$value)=each %$table ){ print qq!$indent"$key":\n!; if(defined(*{$value}{SCALAR}) and defined(${ *{$value}{SCALAR}})){ print "$indent\tSCALAR: ",${*{$value}{SCALAR}}, "\n"; } if(defined( *{$value}{ARRAY} ) ) { print "$indent\tARRAY: ", join ", ",@{*{$value}{ARRAY}}, "\n"; } if(defined( *{$value}{HASH} ) ) { if ($key =~ /::$/){ print "$indent\tSYMTAB:\n"; dumptable($value,"\t".$indent) if (*{$value}{NAME} ne "main::"); }else{ print "\tHASH:\n"; while(my ($k, $v) = each %{ *{$value}{HASH} } ) { print "$indent\t\t$k => $v,\n" } } } if(defined( *{$value}{CODE} ) ) { print "$indent\tCODE: ",*{$value}{CODE},"\n"; }; if(defined( *{$value}{IO} ) ) { print "$indent\tIO: ",*{$value}{IO},"\n"; }; print "\n" } }