Here are the guts of a module that I'm working on called
Globwalker.pm. It's supposed to give you a list of objects
of a given type in a given typeglob (e.g. for example all
subs in main::). It seem to work OK for most glob types,
but I'm getting strange results for scalars. I assume this
is because the scalar knob is defined as soon as the
typeglob comes into existance.
Does anyone have any idea how I can work out if
the scalar has really been used?
package Globwalker;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(get_things get_subs get_scalars get_arrays
get_hashes get_filehandles);
$VERSION = '0.01';
sub get_things {
my $thing = shift;
my $pkg = shift || caller;
my @things;
no strict 'refs'; # WARNING: Deep magic here!
while (my ($sym_name, $sym_glob) = each %{"${pkg}::"}) {
push @things, $sym_name if defined *$sym_glob{$thing};
}
@things;
}
sub get_subs { get_things('CODE', shift || caller) };
sub get_scalars { get_things('SCALAR', shift || caller) };
sub get_arrays { get_things('ARRAY', shift || caller) };
sub get_hashes { get_things('HASH', shift || caller) };
sub get_filehandles { get_things('IO', shift || caller) };
1;
--
<
http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me