in reply to Mr. Ternary is greater than Mrs. If Else

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}

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
    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.

    It's not really a code block. \&{foo} is a reference to the subroutine with the name "foo". Similarly, \& { print "foo" } is a reference to the subroutine with the name that is returned by print, i. e. it's \&1 (unless print fails).

    $ perl -lwe 'my $code = \&{print "foo"}' foo $ perl -lwe 'my $code = \&{print "foo"}; $code->()' foo Undefined subroutine &main::1 called at -e line 1.

    (Note that $code is not called/dereferenced in the first one-liner, yet it prints foo.

      It's not really a code block. \&{foo} is a reference to the subroutine with the name "foo". Similarly, \& { print "foo" } is a reference to the subroutine with the name that is returned by print, i. e. it's \&1 (unless print fails).

      Well, there's a subtle difference. When you write &{foo} -with no quotes within the curlies-, that's (almost) just as if you had written &foo. This is not specific of subs, but of quite about all kind of variables:

      spock:~ [16:02:30]$ perl -wMstrict -le 'my $x=1; print ${x}' Ambiguous use of ${x} resolved to $x at -e line 1. 1 spock:~ [16:02:36]$ perl -wMstrict -le 'my $x=1; print ${"x"}' Can't use string ("x") as a SCALAR ref while "strict refs" in use at - +e line 1.