This construct allows you to use a module only if it's installed. Example with Pod::Usage -- will print full doc if available, message otherwise.
# This is equivalent to 'use Pod::Usage;', except that it does not fai +l if the # module is not available: eval { require Pod::Usage; import Pod::Usage; }; #.... # Usage just calls pod2usage, except in case the Pod::Usage module is +not # available. Then it tries to give some hint on what's going on. # sub usage { if (defined *pod2usage{CODE}) { pod2usage (@_); } else { print STDERR "Can't print error message because the ", "Pod::Usage Perl module is not available\n"; $"=", "; print STDERR "\nAdditional info: @_\n"; exit 3; }

Replies are listed 'Best First'.
Re: Use a module only if it is available
by chromatic (Archbishop) on Dec 30, 2001 at 08:45 UTC
    Instead of checking for the code slot in the current typeglob, you could check $@.
Re: Use a module only if it is available
by ehdonhon (Curate) on Dec 30, 2001 at 10:43 UTC

    I can't take credit for this idea, but I've seen it done in places, and use it myself. I think I saw it in Term::ReadLine:

    unless ( eval "use Packagename; 1" ) { print "Packagename not available!!\n"; }

    The idea is that if things go wrong with the use statement, then the 1 never gets evaluated and the eval will pass back a false value, but if things go ok, then eval will pass back a true value ( 1, the last statement evaluated).

      The 1 value within the eval statement is unnecessary given that all modules must return a true value upon loading - This is most commonly achieved through the addition of a single statement such as:

      # Code here 1; __END__

       

      perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011101"))'