Don't you dare to sow strife between Mrs. If Else and Mr. Ternary!

They've lived together a couple of years now, and I will not let you induce quarrel amongst them over 'who is greater'!

Both serve different purposes. Mrs. If Else provides for blocks of code to be executed conditionally, whereas Mr. Ternary shortcuts them with a single statement (if they resolve to simple expressions) or delegates them elsewhere (probably to Mrs. If Else again, appearing in some sub).

They often interchange tasks, as most mordern couples do; but mostly Mr. Ternary likes to prepare hesh bugs while Mrs. If Else studies blocks of stock exchange charts to see what's best.

I'd rewrite your Mr. Ternary code like this

#!perl/bin/perl use strict; print "Are you sure you want to quit ('yes' or any key for 'no'): "; chomp($_ = <STDIN>); print /\byes\b/i ? <<LEAVE : <<STAY; Press any key to exit... LEAVE Thanks, you stayed... But then, goodbye. Press any key to proceed... STAY <STDIN>; print "GOOD DAY\n";

to better show where Mr. Ternary shines - inlining a decision.

Now, to your delimma (++ alone for mispelling dilemma twice in one post :-):

In the expression

$foo = 1; $foo ? \& { print "foo"} : \& { print "bar"};

The ampersand & marks the following { } construct as a code block; the backslash disambiguates that operation from binary '&' (the bitwise AND) and returns a reference for that code block. Last, Mr. Ternary happily evaluates those references based on the 'truthness' of $foo. This is, at least, my interpretation of things, but I might be wrong. I never used that construct before...

update And I am wrong! See betterworld's comment below.

Nice post!

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: Mr. Ternary is greater than Mrs. If Else by shmem
in thread Mr. Ternary is greater than Mrs. If Else by PerlPhi

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.