What's the use case which justifies extending Perl's codebase for such an exotic feature?

Concise expressiveness. The ternary ? : is a shortcut for a more convoluted simple if/else

my $var; if($cond) { $var = 'this'; } else { $var = 'that'; }

to be written as

my $var = $cond ? 'this' : 'that';

which is one line compared to 5, 6 or 7, depending on indentation style.

We have more of such subtle operators, eg. ||= vs. //=, where the first checks the LHS truthiness, and the second checks the LHS definedness.

Writing

$var = $var ? 'this' : 'that';

just looks and feels as silly as

$value = $value * 5; $next = $next + 1;

instead of

$value *= 5; $next++;

For the same reasons, I like the compound operator x!! so much, because it lets me set up a parameter list based on truthiness of variables:

$result = build_shed( logs => 24, (screws => 120) x!! @screwdrivers, (nails => 360) x!! @hammers, );

Otherwise, I'd had to say:

my %materials = (logs => 24); $materials{screws} = 120 if @screwdrivers; $materiasl{nails} = 360 if @hammers; $result = build_shed(%materials);

While the second variant is one line less, I regard the first variant as much more readable, and I don't have to introduce a temporary hash just for the sake of building function arguments. Note that in the second variant, the hash name is misspelt as materiasl at the 'nails' case, small bug caught by strict, but annoying. So, instead of

$var = $var ? 'this' : 'that';

I'd rather like to see

$var ?= 'this' : 'that';

because it binds the condition to the LHS in the same way as ||= and //= do.

I can hardly imagine a reason to map non-Boolean values to a previously Boolean $var without confusing any maintainer.

Who said that $var was boolean? It could hold any value, and this value is checked for truthiness.

Anyways - de gustibus non est disputandem.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re^3: RFC: "assignary" operator ?= : by shmem
in thread RFC: "assignary" operator ?= : by richard.sharpe

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.