Good point on not needing quoting for the eval.
But no, you don't want the BEGIN in this case. The module
won't be required unless a certain option is specified on
the command line. So the code must compile even without
the module so you can't write the code such that it makes
use of prototypes or predeclaration of subroutines (unless
you want to duplicate those predeclarations in your code).
So you want something like:
if( need_module_X() ) {
if( ! eval { require Module::X; 1 } ) {
die "You can't use feature X because Module::X is not installe
+d.\n";
} else {
Module::X->import( qw( A B C ) );
}
}
Note that I added "; 1" because I don't care to memorize
or rely on which statements return a true value on success.
Note that you can still import routines so that you can
call them via A() instead of Module::X::A().
If you do want/need prototypes or predeclaration of
subroutines, then you could consider using autouse,
though that doesn't give you the custom error message.
Otherwise it would look something like:
BEGIN {
if( ! eval { require Module::X; 1 } ) {
die "You can't use feature X because Module::X is not installe
+d.\n"
if need_module_X();
sub A { croak "Not available" };
sub B(\%); *B= \&A;
sub C(); *C= \&A;
} else {
Module::X->import( qw( A B C ) );
}
}
Untested and unliked. (:
-
tye
(but my friends call me "Tye") |