Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Answer: Defining a subroutine one of two ways depending on whether a package exists

( #86853=categorized answer: print w/ replies, xml ) Need Help??

Q&A > subroutines > Defining a subroutine one of two ways depending on whether a package exists contributed by ariels

If you'd like to separate the use Package; command from the test, you can still do it.

Use the %INC hash (see perlvar). You need to look for a filename, though, not the package name:

eval { use Compress::Zlib; } # ... much other code, or in some other module... BEGIN { *myCRC = (exists $INC{'Compress/Zlib.pm'}) ? sub { crc32(@_) } : sub { 0 }; }

Comment on Answer: Defining a subroutine one of two ways depending on whether a package exists
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2014-08-01 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (33 votes), past polls