Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: dumb question

by hv (Prior)
on Mar 16, 2003 at 03:37 UTC ( [id://243411]=note: print w/replies, xml ) Need Help??


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

Not a dumb question, by any means. I'd say the middle one prints nothing, because $a-- returns the value of $a before the decrement, ie undef. I can think of no good reason, however, for the first to print 0 - that looks like a bug to me.

Looking at the source, there appears to be some special code in the handling of post-increment to cater for this, and it has been there for a long time - at least as far back as perl5.003. But there isn't any indication of a reason for this special handling, and removing it causes only one test failure that doesn't look to be intentionally testing this feature.

I've reported this to the perl development mailing list along with a suggested patch to see if anyone has an idea why this special-case should exist.

Hugo

Replies are listed 'Best First'.
Re: Re: dumb question
by pg (Canon) on Mar 16, 2003 at 05:42 UTC
    I agree that this is a bug. But what is a better fix? I see two different potential fixes, with one common objective to make the interpretation of "print $a--" and "print $a++" consistant.

    1. make both to follow what is happening to post -- now, that is to treat $a as uninitialized, and issue a warining. This might be the only fix for some other languages in a similar case, but not Perl, and I personaly don't like this fix.
    2. Perl is special, and it is not uncommon for Perl to automatically initialize variables for you. Whatever it is post --, or post ++, Perl can predict $a as number, so Perl can just go ahead, and initialize $a to 0. This fix sounds more perlish to me, and I feel that it has a much greater consistancy with the rest part of Perl.

    Try this code:
    $a --; # also try to coment this, and uncomment the next staement #$a ++; print $a; #either -1 or 1, depends on what you uncomment
    In the above demo, whatever it is post --, or post ++, perl would initialied $a to 0, and this is what should happen to print $a-- and print $a++.

      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.

        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.
Re: Re: dumb question
by hv (Prior) on Mar 18, 2003 at 02:03 UTC

    Update: after a fascinating debate on the mailing list (though not quite as wide-ranging as the one here:), the existing behaviour has been clarified as deliberate and desirable, and the documentation, code and tests have been amended to reflect that.

    Hugo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found