in reply to Use a module only if it is available

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).

Replies are listed 'Best First'.
Re: Re: Use a module only if it is available
by rob_au (Abbot) on Jan 19, 2003 at 12:25 UTC
    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"))'