in reply to Re: Re: dumb question
in thread post-increment and post-decrement behavior (was: dumb question)

No, #1 is the correct fix. The postdecrement must return the value from before the decrement operation, which is undef.

In your "fix" #2, how would you handle this situation:

$a = 'a'; print $a--;

By your logic, it would also have to print '0', as Perl would predict it to be a number, and after the decrement $a is -1.

However, I think it is pretty obvious that Perl must print 'a' here.

Replies are listed 'Best First'.
Re: Re: Re: Re: dumb question
by pg (Canon) on Mar 16, 2003 at 06:13 UTC
    Obviously wrong. ;-) This is kind of apple and orange thing. The analogy you drew here is misleading ;-)

    1. In the case you given, there is no space left for prediction, as the value for $a is SET, CLEARLY SET. What kind of prediction is needed? why any prediction is needed? Why any kind of prediction is allowed?
    2. In the case I gave, i.e. the case which this thread is all about, the value of $a is undef, NOT SET, so there is this wild space left for perl to predict. And as I pointed out Perl does this kind of prediction all the time, when it is reasonable, not when it is obviously unreasonable.
      No, you are wrong. :)

      Seriously, in your example $a is undef, which already has a meaning. If you look into perlsyn.pod, you'll find:

      A variable holds the undefined value (C<undef>) until it has been assigned a defined value, which is anything other than C<undef>. When used as a number, C<undef> is treated as C<0>; when used as a string, it is treated the empty string, C<"">; and when used as a reference that isn't being assigned to, it is treated as an error. If you enable warnings, you'll be notified of an uninitialized value whenever you treat C<undef> as a string or a number. Well, usually.

      Printing provides a string context, so $a must be treated as the empty string, and not as 0. That $a becomes a number after the print statement has completed is totally irrelevant in this discussion.