Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

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

Not putting down the way you do it, but just checking $@, particularly without checking what's in it could be problematic in some cases (ie. the variable could potentially be set during this check, unrelated to your eval(), which would technically trigger a false-positive failure). Any eval error happening in the require could also fail with an error, but your eval will reset this var to undef as soon as it starts. Here's a very similar way that does kind of the same thing, but checks the state of the eval, not whether there's an error. It also uses block eval as opposed to string eval:

BEGIN { # look for JSON::XS, and if not available, fall # back to JSON::PP to avoid requiring non-core modules my $json_ok = eval { require JSON::XS; JSON::XS->import; 1; }; if (! $json_ok){ require JSON::PP; JSON::PP->import; } }

What happens there is that if any of the lines prior to the 1; in the eval fail, the eval block will return false (undef to be specific). If all expressions within the eval succeed, the $json_ok variable will be true, and we can proceed confidently.

See a number of issues with eval in the Try::Tiny documentation (which could be, after all, a different approach to the OP's problem. To be honest, I've never used said distribution that I recall).


In reply to Re^2: Help Changing use into require/import by stevieb
in thread Help Changing use into require/import by computergeek

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



  • 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?
    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 perusing the Monastery: (3)
    As of 2021-04-17 18:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?