You can put code refs in @INC, which means you could do this automatically, rather than having to wrap your list of modules...
package MissingModules;
my %missing = ();
BEGIN {
push( @INC, sub {
my ( $code, $mod ) = @_;
$mod =~ s#/#::#g;
$mod =~ s/\.pm$//;
$missing{ $mod }++;
open( my $fh, '+>', undef );
print {$fh} "package $mod;\n1;\n";
seek( $fh, 0, 0 );
return $fh;
} );
}
INIT {
if ( my @list = keys %missing ) {
warn "Please install CPAN modules: cpan -i @list\n";
exit;
}
}
1;
Then in your other code:
#!/usr/bin/perl
use MissingModules;
use CGI;
use Foo;
use Bar;
use Baz;
1;
When you run it, you will get...
% perl test.pl
Please install CPAN modules: cpan -i Bar Baz Foo
We're not surrounded, we're in a target-rich environment! |
---|