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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I don't recall ever using the first one:

$var && sub();

But I don't have any problems with it -- it's clear enough.

Generally I prefer to use the second one wherever possible, tho from habit I always put parentheses around the condition:

sub() if ($var);

The only time I use the third one all on one line is for stuff like:

foreach (@ray1) { push @ray2,$_ if ($_<0) }

Or if there is an else beginning the next line.

I don't understand how having the condition to the right of the action could confuse someone -- I don't think about a line's complete meaning until I've read the whole thing anyway, and it's completely commonplace and normative in natural languages to use subordinate clauses which qualify the previous phrase (ie, your mind should not have difficulty with this). However, I'll admit I'd just as soon do this:

if ($var) sub();

Which is normative in most other languages that I use, such as C, but perl does not allow it.

I presume Larry Wall, linguist and C programmer, had some real reason for this "action before condition" structure in perl and I would doubt it has to do with interpreter internals, so perhaps he saw it as adding something meaningful. Which it does: you can tell something about the nature of this statement because of it's limitations:

action if condition

Namely, that action and condition are both singular and there can be no else clause involved. In other words, having the condition terminate the line indicates that nothing further depends upon it's evaluation. So, instead of adding ambiguity or confusion, I would say number two is (potentially) the opposite: it reduces possible ambiguity because the conditions of it's use are more specific. I guess that "potential" depends on how you choose to respond to it.


In reply to Re: A matter of style: how to perform a simple action based on a simple condition? by halfcountplus
in thread A matter of style: how to perform a simple action based on a simple condition? by muba

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 having a coffee break in the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found