At 13:45 -0500 2003.01.17, John Saylor wrote: >I have a project where I am trying to dump all the variable names and >values in a given package namespace. I can get all the variable names >OK, it's just getting the values from those names that I am not able to >do. > >I have some code like this: >foreach $key ( keys %main::Package:: ) { > $val = $WW::Package::{$key}; > print "$key\t$val\n"; >} > >but all I get is typeglobs.
Here is one way: #!perl -w use strict; for my $name ( keys %strict:: ) { no strict 'refs'; my $val = *{"strict::$name"}{SCALAR}; next if !defined $val || !defined $$val; print "\$strict::$name = $$val\n"; } Produces: $strict::VERSION = 1.01 Peek inside the glob for the value you want (in this case, SCALAR), and if it is defined, deref it and print it. Note that the above works for scalars only. Here's a more robust version handling arrays and hashes too, using even more soft references, with Time::localtime as the package. #!perl -w use Time::localtime; no strict 'refs'; my $pkg = 'Time::localtime'; for my $name ( keys %{$pkg . '::'} ) { for my $type ( qw(SCALAR ARRAY HASH) ) { my $val = *{ "${pkg}::$name" }{ $type }; # val is a reference next unless defined $val; if (ref $val eq 'SCALAR') { next unless defined $$val; print "\$${pkg}::$name = $$val\n"; } elsif (ref $val eq 'ARRAY') { next unless @$val; my $data = join "', '", @$val; print "\@${pkg}::$name = ('$data')\n"; } elsif (ref $val eq 'HASH') { next unless scalar %$val; my $data = join "', '", %$val; print "\%${pkg}::$name = ('$data')\n"; } } } Produces: @Time::localtime::ISA = ('Exporter', 'Time::tm') @Time::localtime::EXPORT = ('localtime', 'ctime') %Time::localtime::EXPORT_TAGS = ('FIELDS', 'ARRAY(0x50498)') $Time::localtime::VERSION = 1.01 @Time::localtime::EXPORT_OK = ('$tm_sec', '$tm_min', '$tm_hour', '$tm_mday', '$tm_mon', '$tm_year', '$tm_wday', '$tm_yday', '$tm_isdst') Note that it is not recursive, so you get an ARRAY ref there. And of course, there are many ways the formatting could be messed up by the data. Data::Dumper to the rescue! #!perl -w use Data::Dumper; use Time::localtime; no strict 'refs'; my $pkg = 'Time::localtime'; my %syms; for my $name ( keys %{$pkg . '::'} ) { for my $type ( qw(SCALAR ARRAY HASH) ) { my $val = *{ "${pkg}::$name" }{ $type }; # val is a reference next unless defined $val; if (ref $val eq 'SCALAR') { next unless defined $$val; } elsif (ref $val eq 'ARRAY') { next unless @$val; } elsif (ref $val eq 'HASH') { next unless scalar %$val; } $syms{$type}{$name} = $val; } } print Dumper \%syms; Produces: $VAR1 = { 'ARRAY' => { 'ISA' => [ 'Exporter', 'Time::tm' ], 'EXPORT' => [ 'localtime', 'ctime' ], 'EXPORT_OK' => [ '$tm_sec', '$tm_min', '$tm_hour', '$tm_mday', '$tm_mon', '$tm_year', '$tm_wday', '$tm_yday', '$tm_isdst' ] }, 'SCALAR' => { 'VERSION' => \'1.01' }, 'HASH' => { 'EXPORT_TAGS' => { 'FIELDS' => [ '$tm_sec', '$tm_min', '$tm_hour', '$tm_mday', '$tm_mon', '$tm_year', '$tm_wday', '$tm_yday', '$tm_isdst', 'localtime', 'ctime' ] } } }; -- Chris Nandor [EMAIL PROTECTED] http://pudge.net/ Open Source Development Network [EMAIL PROTECTED] http://osdn.com/ _______________________________________________ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm