Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Pre vs Post Incrementing variables

by repellent (Priest)
on Sep 12, 2010 at 20:04 UTC ( [id://859904]=note: print w/replies, xml ) Need Help??


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

    $i = 0; ++$i = 'fred';; Can't modify preincrement (++) in scalar assignment

    So, not an lvalue.

Hmm. I was looking at this prior to my previous post. Notice that:
do { my $i = 16; ${ \(++$i) } = 'fred'; $i }; # returns 'fred' do { my $i = 16; ${ \($i++) } = 'fred'; $i }; # returns 17

Maybe the warning you got is due to how Perl parses the preincrement assignment expression? I don't know.

Replies are listed 'Best First'.
Re^5: Pre vs Post Incrementing variables
by BrowserUk (Patriarch) on Sep 13, 2010 at 01:26 UTC

    Waddayaknow! And I've even found a use for it:

    print map ${ \( ++$i ) } %= 10, 1..30;; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0

    Now all we've gotta do is document it, and we've a valid reason for it working that way: measuring golf games. Perfect :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: Pre vs Post Incrementing variables
by BrowserUk (Patriarch) on Sep 13, 2010 at 02:00 UTC

    Mind you, it might make explaining this one a tad awkward :)

    $i = 0; print ++${\++$i}, 0+ ++$i, 0+ ++$i, ++${\++$i};; 6 3 4 6

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      The argument list to print is evaluated from left-to-right. ++${\++$i} is merely incrementing $i twice and returning an alias to $i.
      $i = 0; print ++${\++$i}, 0+ ++$i, 0+ ++$i, ++${\++$i};; ^ ^ ^ ^ (1) (2) (3) (4)

      (1) & (4) return aliases to $i. Since $i has been incremented six times, we get 6 for both of them.

      (2) & (3) return copies to $i because of the zero additions 0+. We get 3 for (2) after three increments, and 4 for (3) with one more increment.

        You appear to be saying that the ability to describe what the implementation does, justifies why it does it that way.

        The fact that this:

        $i=0; print $n = ++$i, $m = ++$i;; 1 2
        produces a different result to this:
        $i=0; print ++$i, ++$i;; 2 2

        Is just plain weird. Some, including me, would say 'broken'.

        Justifying the weirdness by saying "that's what it does", kinda reminds of Apple's "You're holding it wrong." explanation of Antennagate.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2025-07-12 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.