Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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


Description:

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/Zlib.pm'}) ? 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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    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 studying the Monastery: (7)
    As of 2014-10-02 10:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (52 votes), past polls