2

基于Test for exist of perl mod inside script中提供的解决方案,我提出了以下测试:

my @mods = (qw(Cwd DBI Date::Calc Term::ReadLine File::Copy Digest::Perl::MD5
               LWP::Simple Switch POSIX Text::LevenshteinXS File::Spec
               File::Basename String::CRC32 MIME::Lite IO::Socket::INET));

foreach my $mod (@mods) {
    ( my $fn = "$mod.pm" ) =~ s|::|/|g;    # Foo::Bar::Baz => Foo/Bar/Baz.pm
    if ( eval { require $fn; 1; } ) {
        print "Module $mod loaded ok\n";
    } else {
        print "Could not load $mod. Error Message: $@\n";
    }
}

当我最初有 时use something,有时我也有进口,我现在发现它不适用于require,即:

use Digest::Perl::MD5 'md5_hex';

测试后@mods,我想加载它们,所以:

require Cwd;
require DBI;
require Date::Calc;
require Term::ReadLine;
require File::Copy;
require Digest::Perl::MD5 'md5_hex';
require LWP::Simple;
require Switch;
require POSIX;
require Text::LevenshteinXS qw(distance);
require File::Spec;
require File::Basename;
require String::CRC32;
require MIME::Lite;
require IO::Socket::INET;

但这会在运行时引发错误:

syntax error at ./mod.pl line 78, near "require Digest::Perl::MD5 'md5_hex'"
syntax error at ./mod.pl line 82, near "require Text::LevenshteinXS qw(distance)"

我怎样才能做到这一点?

编辑:对于以后偶然发现此线程的其他人,这是工作代码:

use strict;

$| = 1;

################################################
# Help user if Perl mods are missing
################################################
my @mods = (qw(DBI Date::Calc Term::ReadLine File::Copy Digest::MD5 LWP::Simple Text::LevenshteinXS File::Spec String::CRC32 MIME::Lite IO::Socket::INET Getopt::Long));

foreach my $mod (@mods) {
    ( my $fn = "$mod.pm" ) =~ s|::|/|g;    # Foo::Bar::Baz => Foo/Bar/Baz.pm
    if ( eval { require $fn; 1; } ) {
        ##print "Module $mod loaded ok\n";
    } else {
        print "You are missing a required Perl Module: $mod\n";
        my $ok = &getYN( "Shall I attempt to install it for you?", "y" );
        if ( $ok =~ /[Yy]/ ) {
            require CPAN;
            CPAN::install($mod);
        } else {
            print "LogZilla requires $mod\n";
            exit;
        }
    }
}

use feature "switch";
use Cwd;
use File::Basename;
use POSIX;
require DBI;
require Date::Calc;
require Term::ReadLine;
require File::Copy;
require Digest::MD5->import("md5_hex");
require LWP::Simple;
require Text::LevenshteinXS->import("distance");
require File::Spec;
require String::CRC32;
require MIME::Lite;
require IO::Socket::INET;
require Getopt::Long;

my ($autoyes);

sub getYN {
    unless ( $autoyes =~ /[Yy]/ ) {
        my ( $prompt, $default ) = @_;
        my $defaultValue = $default ? "[$default]" : "";
        print "$prompt $defaultValue: ";
        chomp( my $input = <STDIN> );
        return $input ? $input : $default;
    } else {
        return "Y";
    }
}

sub prompt {
    my ( $prompt, $default ) = @_;
    my $defaultValue = $default ? "[$default]" : "";
    print "$prompt $defaultValue: ";
    chomp( my $input = <STDIN> );
    return $input ? $input : $default;
}
4

1 回答 1

9

您需要调用模块的导入方法,例如

Digest::Perl::MD5->import("md5_hex");
Text::LevenshteinXS->import("distance");

use Module完全等同于

 BEGIN { require Module; Module::->import( LIST ); }
于 2012-04-07T14:59:23.957 回答