Re: [Boston.pm] getting values out of symbol tables

2003-01-17 Thread Chris Nandor
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 

Re: [Boston.pm] getting values out of symbol tables

2003-01-17 Thread John Saylor
Hi

( 03.01.17 13:56 -0500 ) Dan Sugalski:
> Typeglobs are all that's in symbol tables. You need to look in the 
> globs if you want to get values.

But how do I do this? Do I open () them? Do I dereference them? I've
been digging through the camel book [and writing test scripts] without
success. Any pointers or suggestions welcome.

-- 
.--- ...

___
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm



Re: [Boston.pm] getting values out of symbol tables

2003-01-17 Thread Jeremy Muhlich
On Fri, 2003-01-17 at 13:45, 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.

Check out the Dumpvalue module.  :)


 -- Jeremy

___
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm



Re: [Boston.pm] getting values out of symbol tables

2003-01-17 Thread John West
On Fri, 2003-01-17 at 13:45, John Saylor wrote:
> Hi
> 
> 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.
> 
> Any help is appreciated.

Dereference the typeglob:

foreach $key ( keys %main::Package:: ) {
  my $scalar   = $$main::Package::{$key};
  my $arrayref = \@$main::Package::{$key};
  my $hashref  = \%$main::Package::{$key};
}

-- 
John West  Software Consultant, Perl Monk
Corporate Technologies, Inc. 781-791-2118



signature.asc
Description: This is a digitally signed message part