Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^10: Pre vs Post Incrementing variables

by shmem (Chancellor)
on Sep 13, 2010 at 14:30 UTC ( [id://859995]=note: print w/replies, xml ) Need Help??


in reply to Re^9: Pre vs Post Incrementing variables
in thread Pre vs Post Incrementing variables

The justification for many of the anomalies that exist in Perl, is that there are one or more very common cases where the anomalous behaviour is useful, Because it allows the capture, within a concise idiom, a piece of behaviour that is sufficiently commonplace to warrent it. This has no such justification.

Well, it might be a common golf case :-D

perlop states

Terms and List Operators (Leftward)
A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses, and any function whose arguments are parenthesized. Actually, there aren’t really functions in this sense, just list operators and unary operators behaving as functions because you put parentheses around the arguments.

If that statement was correct, then in the case of sub { }->(++$c, $c++) pre- and postincrement would conflate to simple increments of the variable after the sub returned, since -- and ++ have lower precedence than TERM according to the docs, no matter whether pre or post.

But for arguments to a sub call, pre-increment seems to mean "increment before sub call" ($var done, can be passed as alias), postincrement "increment after sub call" ($var must be retained for ++, copy is passed) - which, while intuitive, isn't documented anywhere afaics.

But then, one could argue that

perl -E '$c=1; sub{$_[0]+=5}->($c++);say $c'

should yield 7 instead of 2. Ah, well... ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://859995]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found