Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

One way to do this is:

BEGIN { unless ( eval 'use DesiredModule; 1' ) { require AlternateModule; AlternateModule->import(); } }

However, I've also made the argument that using two different modules is not necessarily the best approach. There's ways to get modules onto systems where you don't have permissions (local::lib or even App::FatPacker), plus if you're going to be writing your own module anyway, why not just always use that?

Updates: Added the ->import call, as per choroba's post. It's often not required for OO modules. If you need to pass a specific import list, you can do eval 'use DesiredModule qw/some_func other_func/; 1' and AlternateModule->import(qw/some_func other_func/);. Another possible variation is that you can replace the inner block require ... ->import(); with eval 'use AlternateModule; 1' or die $@||'unknown error';. Stringy eval is often the wrong choice, but in this case it's fine because it's using a fixed string of code.


In reply to Re: use alternate module if pm is not installed by haukex
in thread use alternate module if pm is not installed by agweih

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2021-09-26 01:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?