On Fri, 18 Oct 2002, Simon Wistow wrote: > On Thu, Oct 17, 2002 at 10:24:18PM +0100, Roger Burton West said: > > I have a system with a lot of classes, all in the same namespace - > > Foo::Bar, Foo::Baz, Foo::Qux, etc. I'm expecting users of the program to > > add more classes; these may be in the main program or in additional > > files. Is there a way in which the program can get a list of > > the names of all Foo::* classes that have been defined? > > I use : > > use File::Find::Rule qw/find/; > use File::Basename; > > my @dirs; > > foreach my $dir ( map { "$_/Foo/" } @INC ) { > push @dirs, $dir if ( -e $dir && -d $dir ); > } > > my @files = find( name => "*.pm", in => \@dirs ); > my @modules; > > foreach my $file (@files) { > my ($name) = fileparse($file, "\.pm"); > push @modules, $file;
Why not $modules{$file} = 1 already and lose the list? It seems silly to build a list of duplicates by iteration and then convert it into a hash. > } > > my %modules = map { $_ => 1 } @modules; > > return sort keys %modules; > > > > > Which effectively allows you to have plugins. That code above is ripped > out of Siesta and if you subsitute 'Foo' for Siesta/Plugin then it > allows anybody to install a plugin in the namespace Siesta::Plugin:: and > have it picked up on the system. > > As such it does something slightly different to what Shevek suggested. > > Simon > > > -- Shevek I am the Borg. sub AUTOLOAD{my$i=$AUTOLOAD;my$x=shift;$i=~s/^.*://;print"$x\n";eval qq{*$AUTOLOAD=sub{my\$x=shift;return unless \$x%$i;&{$x}(\$x);};};} foreach my $i (3..65535) { &{'2'}($i); }