Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

( #86670=categorized question: print w/ replies, xml ) Need Help??
Contributed by bikeNomad on Jun 07, 2001 at 22:25 UTC
Q&A  > subroutines


I'd like my subroutine to use an optional package but only if it exists.

But if I try to do a 'use OptionalPackage;' my program dies if it doesn't exist. How do I deal with this?

Answer: 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/'}) ? sub { crc32(@_) } : sub { 0 }; }
Answer: Defining a subroutine one of two ways depending on whether a package exists
contributed by bikeNomad

You can do something like this:

BEGIN { eval { require Compress::Zlib; import Compress::Zlib 'crc32'; }; *myCRC = $@ ? sub { 0 } # don't have it : sub { crc32(@_) }; }
This calls Compress::Zlib::crc32 or just returns 0.

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.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2016-08-25 20:38 GMT
    Find Nodes?
      Voting Booth?
      The best thing I ever won in a lottery was:

      Results (362 votes). Check out past polls.