in reply to Mr. Ternary is greater than Mrs. If Else
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}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Mr. Ternary is greater than Mrs. If Else
by betterworld (Curate) on May 20, 2007 at 01:11 UTC | |
by blazar (Canon) on May 20, 2007 at 14:03 UTC |