Personally, I often nest ternaries when faced with such a case.
$foo ? do { bar($_) for 1..3; } : $baz eq $quux ? (warn, return -1) : $answer == 42 ? print "but what's the question?\n" : print "who knows\n";

To my sense of aesthetics, this is quite pleasing. Of course, it only works well if the code for each case isn't very long, otherwise it breaks apart visually.

The only thing I don't like about it is the default case's treatment in formatting - I can't decide whether to line it up, indent it, or how to otherwise make it obvious as default case. Right now, I'm thinking unconditionally wrapping it in a do block and leaving it at the same indentation level as the conditionals (which is where it logically belongs - it just doesn't seem to stand out enough by itself). As in

$foo ? do { bar($_) for 1..3; } : $baz eq $quux ? (warn, return -1) : $answer == 42 ? print "but what's the question?\n" : do { print "who knows\n" };
I quite like this solution now that I'm looking at it. Maybe I can finally make up my mind.

Makeshifts last the longest.


In reply to Re: Pseudo-switch question by Aristotle
in thread Pseudo-switch question by fletcher_the_dog

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.