Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Incrementing a Hash Value

by marvell (Pilgrim)
on Jun 14, 2002 at 07:39 UTC ( [id://174460]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Incrementing a Hash Value
in thread Incrementing a Hash Value

Is it undefined, or does do this:
# basic form $i = $i++; # long hand $k = $i; $i++; $i = $k;

--
¤ Steve Marvell

Replies are listed 'Best First'.
Re: Incrementing a Hash Value
by Abigail-II (Bishop) on Jun 14, 2002 at 07:56 UTC

      I'm definitely not qualified to argue whether this behavior should be considered 'undefined', but I haven't seen any pointers to documentation that says that it is.

      perlop is written in a language which is a little less than explicit than Perl, but it doesn't say that ++ and -- work exactly as in C.

      In fact, perlop says:
      "++" and "--" work as in C. That is, if placed before a variable, they increment or decrement the variable before returning the value, and if placed after, increment or decrement the variable after returning the value.

      In other words, the auto-increment operator works as in C in the specific way mentioned. (perlop then goes on, in the next paragraph, to explain one way in which the perl implementation differs from C.)


      Impossible Robot
Re: Re: Re: Incrementing a Hash Value
by Juerd (Abbot) on Jun 14, 2002 at 10:29 UTC

    Is it undefined, or does do this: $k = $i; $i++; $i = $k;

    Both.

    $i++ stores the old value, increases the value, and then sets the return value to the stored old value. Although that is true for all versions of Perl, its behaviour is not documented. Actually, it is documented, but wrong:

    [...] increment or decrement the variable after returning the value. [...] -- perlop
    It doesn't increment after returning. It increments first, and then returns the old value.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      That sounds well defined to me. If it has a predicible behaviour, even if it's not useful, it's defined.

      I must be missing something.

      --
      Steve Marvell

        But it isn't well defined. In
        $i = $i ++;
        you do not know whether the assignment will happen before, or after the increment.

        Abigail

      It doesn't increment after returning. It increments first, and then returns the old value.
      No, and that's the entire problem. You do not know when the increment will happen, that is specifically left undefined. (It was left undefined in C to give implementers of compilers all sorts of freedom to optimize it - idem for Perl). What a specific version of Perl does not gives you absolutely no garantee what it will do in a next version.

      Abigail

        No, and that's the entire problem. You do not know when the increment will happen, that is specifically left undefined.

        It is documented to be undefined (as you can't know when "after" will be), but the documentation is wrong ("before" is reality: the old value is stored in a temporary SV).

        (It was left undefined in C to give implementers of compilers all sorts of freedom to optimize it - idem for Perl)

        Perl will never be able to optimize $foo++ for real scalars, because they can be tied or overloaded at runtime. With Perl 6, when there are variables that are numbers but can never become a real scalar, maybe it can be optimized.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174460]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.