Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

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

G'day Don Coyote,

if may be used as a Compound Statement or as a Statement Modifier. It is not an operator.

Placing an if statement in a do block, as you show, can emulate the ternary operator but, as is fairly obvious, results in clunky code with poor readability and maintainability.

So, if introduces a compound statement (or statement modifier) and ?: is an operator. They are not synonymous. With do blocks, parentheses, and other syntactical tricks, you can usually (if not always) replace one with the other; however, this is typically not a good idea. [For more on this, see "Re: Code blocks with ternary operator or trailing conditionals", which I wrote a few weeks ago.]

The ternary operator can be used in any list, not just to generate an array, so an improvement on the print examples you show, might be (using equivalent code):

print 1<2 ? '1' : '2', $/;

Although, perhaps a more likely scenario might be something like:

$ perl -Mstrict -Mwarnings -e ' for my $item_count (0..2) { print "$item_count item", $item_count == 1 ? "" : "s", ".\n" } ' 0 items. 1 item. 2 items.

When using the ternary operator, take care with precedence issues and add parentheses where needed (the documentation shows some examples).

A final point, unrelated to conditionals, regarding your test:

my $arrref = \( if(1<2){'1'}else{()} );

Taking a reference to a list actually returns a list of references to each item in the original list. Accordingly, it would probably be a good idea to avoid any instances of:

$arrayref = \(...);

While that may work when first written using a list with only one item, any subsequent modifications which result in additional list items will cause problems. A better option would be:

$arrayref = [...];

Item 2. of "perlref: Making References" has more information on this.

-- Ken

In reply to Re: conditional statements in list constructors by kcott
in thread conditional statements in list constructors by Don Coyote

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?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2022-05-27 15:52 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (96 votes). Check out past polls.