Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 surveying the Monastery: (7)
As of 2015-07-04 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls