Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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":



  • 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
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-06-21 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (99 votes). Check out past polls.

    Notices?