-
Notifications
You must be signed in to change notification settings - Fork 597
Description
While Perl doesn't strictly guarantee evaluation order, it usually does pretty well at not surprising you, doing the right thing in most circumstances. In particular, when fetching elements out of array or hash references, or invoking methods:
eval: (do { say "First"; \my @a })->[say "Second"]
First
Second
eval: (do { say "First"; \my %h })->{say "Second"}
First
Second
eval: (do { say "First"; undef })->meth(say "Second")
First
Second
ERR: Can't call method "meth" on an undefined value at (eval 15) line 1.
(Ignore the fact that it died in this last example; the fact remains it evaluated the LHS and printed "First"
before it evaluated the RHS and printed "Second"
)
This neatness does not hold when invoking code references:
eval: (do { say "First"; sub {} })->(say "Second")
Second
First
The casual observer would expect that it would print "First" then "Second" in that order, as it does for the other examples; but yet it does not.
This may be regarded as "just a quirk", but it does have an annoying consequence when we consider Perl/PPCs#23 (comment). A satisfactory solution in the optional-chaining case might involve changing the way that regular (i.e. non-optional) code ref invocation also works, to fix this evaluation order problem.