Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://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":



  • 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 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?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found