Hi,
I wanted to be able to do this in Perl (the code below is Python lol)
try:
import Module
except:
print "You need module Module to run this program."
Does anyone have any idea how to?
Hi,
I wanted to be able to do this in Perl (the code below is Python lol)
try:
import Module
except:
print "You need module Module to run this program."
Does anyone have any idea how to?
eval "use Module; 1" or die "you need Module to run this program".
or
require Module or die "you need Module to run this program";
Module->import;
or
use Module::Load;
eval { load Module; 1 } or die "you need Module to run this program";
You can find Module::Load on CPAN.
You can use Module::Load::Conditional
use Module::Load::Conditional qw[can_load check_install requires];
my $use_list = {
CPANPLUS => 0.05,
LWP => 5.60,
'Test::More' => undef,
};
if(can_load( modules => $use_list ))
{
print 'all modules loaded successfully';
}
else
{
print 'failed to load required modules';
}
use strict; use warnings; use Module;
If you don't have Module installed, you will get the error "Can't locate Module.pm in @INC (@INC contains: ...)." which is understandable enough.
Is there some particular reason you want/need a more specific message?
Something like this, use Net::SMTP
if you have the module installed, or a cheesy sendmail
callout as a last resort.
my $mailmethod = eval "use Net::SMTP; 1" ? 'perl' : 'sendmail';