[VPM] Reflection

abez abez at abez.ca
Thu Jun 26 17:02:53 CDT 2003


Ok that works I figured out my namespace was the problem. I wasn't looking at an
actual package.

Ok then thanks for the code, here's the code to grab all the methods and put
them in a hash (so you can just use keys to get a list). Since we can have
multiple inheritance we have to watch for cycles too.

use strict;
use Data::Dumper;

my @packages =  qw( File::Find );
foreach my $p (@packages) {
	print Dumper(printMethods($p));
}

sub printMethods {
	no strict "refs";
	my ($name,$done) = @_;

	$done = {} unless defined $done;
	$done->{$name} = 1;

	eval "use $name;";
	my $arr = \@{"${name}::ISA"};
	my $methods = { };
	for (keys %{"${name}::"}) {
		$methods->{$_} = 1 if ${name}->can($_);
	}
	foreach my $elm (@$arr) {
		next if $done->{$elm}; #CYCLE
		my $hash = printMethods($elm,$done);
		while (my ($key) = each %$hash) {
			$methods->{$key} = 1;
		}
	}
	return $methods;
}




On Thu, 26 Jun 2003, Nathanael Kuipers wrote:

> The following should do what you want...if this isn't what you tried, what
> about the output is not what you expected?
>
> #!/usr/bin/perl
>
> use File::Find; #example
> for (keys %File::Find::) {
> 	print "$_" if File::Find->can($_);
> }
>

-- 
abez ------------------------------------------
http://www.abez.ca/ Abram Hindle (abez at abez.ca)
------------------------------------------ abez



More information about the Victoria-pm mailing list