[Chicago-talk] Check for modules

Young, Darren Darren.Young at ChicagoGSB.edu
Wed Jan 18 13:11:44 PST 2006


Was reading recipe 12.2 in the cookbook (Trapping Errors in require or
use) and came up with something based on their example. Any thoughts on
this chunk? Possible problems? I'd prefer to spit out more
details/instructions if a module isn't installed as opposed to the
"Can't find foo/bar"

Can't even say how many times I've had a non-perl admin move one of my
scripts to a different host then come back and say "what does this
mean?".

########################################################################
####
#                               B E G I N
########################################################################
####

# Require a Perl version
require 5.006_001;                  # tested against 5.6 and 5.8

# Pragmas
use strict;                         # catch my accidental lack of my

BEGIN {

    # 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);
        }
    }
}

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?

Thanks,

Darren


More information about the Chicago-talk mailing list