eugene y eugene y - 2 months ago 19
Perl Question

Is ~~ a short-circuit operator?

From the Smart matching in detail section in perlsyn:


The smart match operator
short-circuits whenever possible.


Does
~~
have anything in common with short circuit operators (
&&
,
||
, etc.) ?

Answer

The meaning of short-circuiting here is that evaluation will stop as soon as the boolean outcome is established.

perl -E "@x=qw/a b c d/; for (qw/b w/) { say qq($_ - ), $_ ~~ @x ? q(ja) : q(nein) }"

For the input b, Perl won't look at the elements following b in @x. The grep built-in, on the other hand, to which the document you quote makes reference, will process the entire list even though all that's needed might be a boolean.

perl -E "@x=qw/a b c/; for (qw/b d/) { say qq($_ - ), scalar grep $_, @x ? q(ja) : q(nein) }"
Comments