Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

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

In Java, it's easy to know how try { } catch { } block line up by using simple indentation rules:

try { // Do Stuff } catch (IOException e) { // Handle IO errors } catch (Exception e) { // Handle any other error }

The equivilent construct in Perl (without using modules) is to use an eval { }:

eval { # Do stuff }; if($@) { # Handle any errors }

The problem is that it isn't immediately apparent that the if block is equivilent to a Java catch (i.e., something that catches errors, as opposed to one of the infinate number of other uses for an if statement). To a novice Perl coder, it's just an if statement with a weird punctuation variable in the conditional. Even an experianced Perl coder needs to read more information to know what it does, since the Human brain tends to read an entire word at a time.

One could use the Exception Error module (Update: I meant Error.pm here. Thanks Zed_Lopez.), but that module has issues with its implementation and should probably be avoided.

The next best solution that I'm putting forth is to break normal indentation rules:

eval { # Do stuff }; if(@$) { # Handle any errors }

By cuddling the if, and being consistant in not cuddling anywhere else (you are being consistant, right?), you're saying "this if statement is related to the eval above it". To a novice, it still has a weird punctuation variable next to it, but at least they're hinted in as to its true function. To an experianced one, they can recognize the construct without extra intepretation at the HumanOS layer.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated


In reply to Style Point: Catching eval { } errors by hardburn

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!
  • 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 chanting in the Monastery: (11)
    As of 2014-12-21 09:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (104 votes), past polls