in reply to Defining a subroutine one of two ways depending on whether a package exists

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 Re: Defining a subroutine one of two ways depending on whether a package exists
  • Download Code