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

The way this works is:

The BEGIN block forces this to happen at the very beginning of the program. The eval tries to load the package (in this case Compress::Zlib) and import the symbol 'crc32' into the current package. If this process succeeds, the eval will leave a null string in $@.

The *myCRC = $@ ? sub { } : sub { } part sets up the name 'myCRC' in the symbol table of the current package to refer to one of two subroutines that are defined anonymously here.

Originally posted as a Categorized Answer.

  • Comment on Re: Defining a subroutine one of two ways depending on whether a package exists