Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by ariels (Curate)
on Jun 08, 2001 at 11:01 UTC ( #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 - Answer 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?
[stevieb]: choroba Been there, done that :) Better too many than not enough though!
[stevieb]: I very often write a base set of unit tests before writing any code. It helps dictate the API, and ensures my earliest of ideas stay on track.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-07-21 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (335 votes). Check out past polls.