[cgiapp] packages autouse?

Michael Peters mpeters at plusthree.com
Fri Jun 12 09:09:35 EDT 2009


Porta wrote:
> I saw it, but unless I use the superloader, it still need to define the
> names of the modules I want to use.And, regarding the superloader, I'm lazy
> enough to want to use all the packages within a given folder, but not just
> every possible module in the world...
> Still, a good suggestion.

I usually do something similar with some of my mod_perl programs to preload them 
into memory but you could probably do something similar. You could use it like:

use MyProject::AutoUse;

And it could look something like:

package MyProject::AutoUse;
use File::Find qw(find);

BEGIN {
     find(
         {
             wanted => sub {
                 return unless m/\.pm$/;
                 return if /^\.?#/;    # skip emacs droppings
                 my $module = $1;
                 $module =~ s/\//::/g;

                 my $pkg = "MyProject::$module";
                 eval "use $pkg;";
                 die "Problem loading $pkg:\n\n$@" if $@;
             },
             no_chdir => 1
         },
         '/path/to/project/lib/MyProject'
     );
}


caveat: not tested but should give you the general idea.

-- 
Michael Peters
Plus Three, LP



More information about the cgiapp mailing list