Front page | perl.perl5.porters |
Postings from June 2016
Re: [perl #127684] operators '|||' and '&&&'
Thread Previous
|
Thread Next
From:
Aristotle Pagaltzis
Date:
June 20, 2016 05:31
Subject:
Re: [perl #127684] operators '|||' and '&&&'
Message ID:
20160620053128.GA74341@plasmasturm.org
* Ulrich Windl <perlbug-followup@perl.org> [2016-03-10 13:15]:
> While writing a program for perl-5.10, I wondered whether the is an
> AND-equivalent for '//' for a closure like
> sub {
> my $hi = $header_i{(shift)} ??? return $hi;
> die;
> };
>
> Where '???' is the operator that doesn't exist.
First, an aside: under strict vars, this would never work, no matter how
such an operator were spelled, because declarations do not take effect
until after the end of the statement – the `return $hi` part in that
statement will always be a compile-time error.
As for what you asked about, you can achieve this already, by using the
existing defined-or to convert undef to an empty list, which allows you
to use a loop as a conditional:
return $_ for $header_i{(shift)} // ();
(Depending on circumstances you might also use a grep or map instead. Or
a full block loop, which allows you to name the value.)
I’ve used this idiom a fair amount.
> Thus I wondered why the perl gurus didn't choose '|||' instead of
> '//'; then '&&&' would be just the natural thing for the AND-variant.
Who knows. The symmetry argument almost certainly didn’t come up though,
because the AND version is needed much less often than the OR version.
Judging by the fact that //() has become an idiom for me, a defined-and
operator is clearly not a useless idea – evidently I would have used it
plenty, even if less than defined-or.
> Did I overlook something that would have prevented use of those
> operators?
At least &&& is already sometimes valid syntax in contexts where this
new interpretation for it would be applicable, so it’s more ambiguous
than // was.
* H.Merijn Brand <h.m.brand@xs4all.nl> [2016-03-10 13:20]:
> When the defined-or was introduced, there was: the "err" keyword,
> which was implemented as a weak keyword.
That was a low-precedence version of defined-or, so it’s irrelevant to
the OP who was talking about defined-and.
* Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> [2016-03-10 13:55]:
> There's no need for a "defined-and" operator in Perl, because there is
> no undefined value that is true. The regular "and" operators are fine
> in this circumstance.
Incorrect. No undefined value is true, but that doesn’t mean every
defined value is true. For false defined values, the regular `and`
operators yield different results than defined-and would.
* Zefram <zefram@fysh.org> [2016-03-10 15:50]:
> It's not brilliantly useful. // is good for situations where you're
> using undef as a null value in a, and you want to apply a default if
> it's null. It requires only a single undef-as-null slot to be useful.
> defined-and, however spelled, would require a coincidence of two
> undef-as-distinguished-value slots to be useful: a and the output.
> The operation that it computes is not one that naturally arises from
> the undef-as-null arrangement. So I do not think it warrants a new
> punctuation operator.
It’s useful enough that shell has *only* defined-and, and no defined-or.
Shell calls it “alternate value expansion”, which ought to give a clear
indication of the use cases it’s good for.
* bulk88 via RT <perlbug-followup@perl.org> [2016-03-14 19:15]:
> ??? looks like unicode replacement characters in an IDE. Very bad
> choice for an operator.
This seems like it must be humour, in which case it is so bone dry that
it leaves me unable to decide whether or not it is.
Anyway, this ticket should probably be closed.
Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>
Thread Previous
|
Thread Next