It's hard to look outside the box, almost by definition. So what follows aren't really suggestions for syntax, they're more questions for "what's a good idiom for?"

The "loop-and-a-half" problem has already been mentioned, but I still haven't seen anything that looks really good (though the redo aproach may be the cleanest).

Something I've frequently found myself doing is wanting a three-way control structure for greater than, less than, and equal to. Sure there are ways to do it, but none of them really feel clean.

given fork { case $_ < 0 { #error } case $_ > 0 { #parent } default { #child } }
or
if ((my $pid = fork()) < 0){ #error } elsif ($pid > 0) { #parent } else { $child }
But it's the sort of thing that feels like it could be simpler. A better example might be checking two values against each other, where it feels unnatural to need a separate variable or compare them multiple times.
if (get_the_boundary_x() < the_user_provided_x()){ draw_color("green"); elsif (get_the_boundary_x() == the_user_provided_x()){ draw_color("blue"); else { draw_color("red"); }
(Yes, I know that can be done with draw_color(qw( blue red green )[get_the_boundary_x() <=> the_user_provided_x()], but that's just ugly and confusing).

merlyn's looking for a good idiom: return this if this is true shows another question without a (really good, or at least completely natural) answer. The suggested if (my $ret = thatroutine()){return $ret} feels ugly due to the synthetic variable $ret.


In reply to Re: Control Structures by Eimi Metamorphoumai
in thread Control Structures by artist

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.