Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Re: Re: Load all modules in directory

by hanenkamp (Pilgrim)
on Dec 10, 2003 at 19:42 UTC ( [id://313830]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Load all modules in directory
in thread Load all modules in directory

The code given has syntax errors in it, but setting those aside: It-Works-For-Me(tm). Here it is with typos removed:

BEGIN { my $mod_path = "/home/tboyd/lib/site_perl/5.6.1/Tools/"; my @mods = glob("$mod_path*"); for (@mods) { require $_; } }

It should be noted that require is different from use in that use calls the module's import method and require does not. So, this could cause problems later on if you expect symbols to be imported (or these modules do something else nice for you in import).

You may also want to change your glob to end in *.pm if you only want those files as there could be other files in the directory that aren't perlish.

Replies are listed 'Best First'.
Re: Re: Re: Re: Load all modules in directory
by CombatSquirrel (Hermit) on Dec 10, 2003 at 19:48 UTC
    For me, your code results in
    Can't locate C:/Perl/site/lib/ACME in @INC (@INC contains: C:/Perl/lib + C:/Perl/site/lib .) at C:\Dokumente und Einstellungen\foo\Desktop\t. +pl line 10. BEGIN failed--compilation aborted at C:\Dokumente und Einstellungen\fo +o\Desktop\t.pl line 12.
    I just tried to use eval to trap some nasty error messages and I got away with it -- I highly doubt whether this is legal, though:
    #!perl use strict; use warnings; BEGIN { my $dir = "C:/Perl/site/lib/"; my $r; for (<$dir*.pm>) { /([^\/]+)$/ && ($r = $1) or next; eval { rquire $_ } unless ($r and $INC{$r}); } } use Data::Dumper; print Dumper \%INC;

    Anyway, I hope this helped the OP.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.

      The error you point out is part of the reason I suggested changing * to *.pm. Directories won't load very happily. Although, in this case we could add this instead of the *.pm:

      require $_ unless -d $_;

      But, with this solution, we'd still run into problems if there are other odd files like READMEs or something else that got stuck in the directory we are scanning.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://313830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2025-01-13 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (31 votes). Check out past polls.