in reply to Dynamically load unknown modules

Ok, after hacking about a bit I've found this snippet does what i want.

Code review invited (yes i'm going to change the name of "superclass").. :-)

sub post_configure_hook { my $self = shift; $self->logObj( Logger->new(name => 'log/gaa') ); $self->xml( new XML::Simple(xmldecl => 1,forcearray => 1) ); # Go find all the modules find ({no_chdir => 1, wanted => sub { $self->load_api_modules($Fil +e::Find::name) }, follow =>1}, 'api'); } sub load_api_modules{ my $self = shift; my $filename = shift; if ($filename =~ /\.pm$/) { my @superclass = split(/\//, $filename); require "$filename"; my $name = $filename; $name =~ s#/#::#g; # Convert slashes (/) to :: substr($name, -3,3, ''); # Remove the .pm at the end $self->{$superclass[1].$superclass[2]} = new {split(/\./, $sup +erclass[$#superclass])}[0]; $self->logObj->log(location => 'Initialisation', message => 'l +oaded: '.$File::Find::name); } }

Replies are listed 'Best First'.
Re: Re: Dynamically load unknown modules
by fruiture (Curate) on Aug 27, 2002 at 16:31 UTC

    general: long but full lines are always hard to read.

    #example: your call of find() find ( { no_chdir => 1, wanted => sub { $self->load_api_modules($File::Find::name) }, follow =>1 }, 'api');

    Allow me to rewrite them load_api_modules() method:

    sub load_api_modules { my $self = shift; my $file = shift; local $_ = $file;#will work with $_ and keep $file return unless s/\.pm$//; require $file; my @sup = split m#/#; # s#/#::#g; unneccessary $self->{ $sup[1].$sup[2] } = $sup[@sup-1]->new(); $self->logObj->log( location => "Initialisation", message => "loaded: $file" ); }

    HTH

    --
    http://fruiture.de
      cool, thx for a fresh viewpoint.