note
epoptai
You can check for the existence of a module with [eval]:
<code>
eval "use Some::Module";
$@ && print_an_error_message_with_a_nice_link_to_cpan();
</code>
<p><br>
Here's another technique for using non-core modules that fits in better with the other module calls, and is able to report <b>multiple missing modules:</b>
<code>
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use_('Some::Module');
use_('Some::OtherModule');
install_modules() if %_; # if use_ fails this happens
sub use_
{
eval "use $_[0]";
$@ && $_{$_[0]}++;
undef $@
}
sub install_modules
{ # link to dist(s) on cpan
if(keys %_ > 1){ $_ = 's' } else { $_ = '' }
my $p = header;
$p .= qq~Required perl module$_ not found: ~;
for(keys %_){
$p .= qq~<a href='http://search.cpan.org/search?dist=$_'>$_</a>, ~;
}
$p =~ s|, $||;
print $p;
exit
}
</code>
Hope this helps.
<p>
<b>Update:</b> Cleaned it up a bit.
<p><font size="-1">
--<br>
Check out my <A HREF="/index.pl?node=Perlmonks+Related+Scripts">Perlmonks Related Scripts</A> like <A HREF="/index.pl?node=framechat">framechat</A>,
<A HREF="/index.pl?node=reputer">reputer</A>, and <A HREF="/index.pl?node=xNN">xNN</A>.</font>
183364
183364