summaryrefslogtreecommitdiff
path: root/pod
diff options
context:
space:
mode:
authorDan Book <grinnz@grinnz.com>2020-04-28 15:18:18 -0400
committerSawyer X <xsawyerx@cpan.org>2020-05-21 00:45:36 +0300
commit88c28b3819570e2ea7914875b25f0f40654c570d (patch)
treeb925aa45b8edff37758163bee968e7d5e0a17bb6 /pod
parentec89cf10fbe40da4631926631340a9f0d9e49f6a (diff)
downloadperl-88c28b3819570e2ea7914875b25f0f40654c570d.tar.gz
perlop - Add more examples for chained comparisons
The distinction between how many times an expression may be evaluated and how many times its result may be fetched is somewhat confusing. This calls out the distinction earlier in the text rather than just buried in a paragraph, and adds examples to help explain the difference.
Diffstat (limited to 'pod')
-rw-r--r--pod/perlop.pod16
1 files changed, 15 insertions, 1 deletions
diff --git a/pod/perlop.pod b/pod/perlop.pod
index cd87f1ab50..08466e1219 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -71,7 +71,8 @@ it looks. The ANDing short-circuits just like C<"&&"> does, stopping
the sequence of comparisons as soon as one yields false.
In a chained comparison, each argument expression is evaluated at most
-once, even if it takes part in two comparisons. (It is not evaluated
+once, even if it takes part in two comparisons, but the result of the
+evaluation is fetched for each comparison. (It is not evaluated
at all if the short-circuiting means that it's not required for any
comparisons.) This matters if the computation of an interior argument
is expensive or non-deterministic. For example,
@@ -98,6 +99,19 @@ a tied scalar, then the expression is evaluated to produce that scalar
at most once, but the value of that scalar may be fetched up to twice,
once for each comparison in which it is actually used.
+In this example, the subroutine is called only once, and the tied
+scalar it returns is fetched for each comparison that uses it.
+
+ if ($x < get_tied_scalar() < $z) { ...
+
+In the next example, the subroutine is called only once, and the tied
+scalar it returns is fetched once as part of the operation within the
+expression. The result of that operation is fetched for each comparison,
+which normally doesn't matter unless that expression result is also
+magical due to operator overloading.
+
+ if ($x < get_tied_scalar() + 42 < $z) { ...
+
Some operators are instead non-associative, meaning that it is a syntax
error to use a sequence of those operators of the same precedence.
For example, S<C<"$x .. $y .. $z">> is an error.