develooper Front page | perl.perl5.porters | Postings from September 2005

Re: ++ turns undef into 0 first

Thread Previous | Thread Next
From:
Abigail
Date:
September 26, 2005 14:58
Subject:
Re: ++ turns undef into 0 first
Message ID:
20050926215831.GI592@abigail.nl
On Thu, Aug 18, 2005 at 10:49:30AM -0700, Peter Scott wrote:
> This recently bit me:
> 
> $ perl -wle 'print 42 if defined($h{foo}++)'
> 42
> 
> Now, before thousands of you reach for the flamethrowers, yes, I know
> - now - that this is documented.  I found it in perlop right where it
> ought to be:
> 
> | undef is always treated as numeric, and in particular is changed to 0
> | before incrementing (so that a post-increment of an undef value will
> | return 0 rather than undef).
> 
> The only thing that perturbs me is that I had to find this after I was
> starting to wonder whether there was a bug.  So this looks to me like
> suboptimal behavior that has been documented; any chance of making it
> behave the more logical way, i.e., that the expression evaluates to the
> original value and not some modified one?  I can't imagine there is much
> code relying on the current behavior.


Well, I have written code that makes use of this 'feature'. I might
fail if 'undef' was returned, but more likely it would start emitting
warnings.

But all I remember is having used this feature. I've no idea whether
that code is still running somewhere.



Abigail

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About