[Chicago-talk] Check for modules

Steven Lembark lembark at wrkhors.com
Thu Jan 19 15:19:44 PST 2006


>     # modules we need to operate
>     my @MODs = 
>     (
>         # "Standard" modules we use
>         'FindBin',
>         'Getopt::Long',
>         'File::Basename',
>         'Sys::Hostname',
>         'File::stat',
>         'Pod::Usage',
>         'POSIX',
> 
>         # "Extra" modules we need
>         # (not included with the Perl distribution)
>         'Mail::Sender',
>         'Parallel::ForkManager',
>         'Date::Format',
>     );
> 
>     # check to see if the required modules are installed
>     # and if so, load them up otherwise puke
>     for my $mod (@MODs) {
>         if ( eval "require $mod" ) {
>             $mod->import();
>             ### print "Loaded module: $mod\n";
>         } else {
>             print "Module $mod not installed!" and exit(0);
>         }
>     }
> }

Just use them; they'll succeed for fail on their own
and you can't really recover from one of them failing
anyway.
 
> Namely, was wondering about the eval "require" vs. an eval "use" (which
> doesn't seem to produce the same results). Is eval "reqiure" + import()
> the ideal way to do this?

    use foo 

is pretty much the same as:

    BEGIN
    {
        require foo;

        foo->import;
    }

This leaves out a sanity check for import existing among
other things.

require doesn't call the import hook for you and
is done at runtime (vs. in a begin block).

-- 
Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508


More information about the Chicago-talk mailing list