develooper Front page | perl.perl5.porters | Postings from April 2008

Re: bug or not? constants warn only once

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
April 24, 2008 10:24
Subject:
Re: bug or not? constants warn only once
Message ID:
20080424192350.342b4ffa@pc09.procura.nl
On Thu, 24 Apr 2008 17:36:21 +0100, Nicholas Clark <nick@ccl4.org>
wrote:

> $ perl -lwe 'sub pie {print 0 + "pie"}; pie; pie;'
> Argument "pie" isn't numeric in addition (+) at -e line 1.
> 0
> 0
> 
> 
> This surprised me. I assumed that I should get a warning for each call to
> the subroutine. But looking at the implementation, it's obvious why it doesn't
> warn twice:
> 
> perl -MDevel::Peek -lwe 'sub pie {Dump $_[0]; print 0 + $_[0]; Dump $_[0]}; pie "pie" for 0,1'
> SV = PV(0x801174) at 0x80021c
>   REFCNT = 1
>   FLAGS = (POK,READONLY,pPOK)
>   PV = 0x2017c0 "pie"\0
>   CUR = 3
>   LEN = 4
> Argument "pie" isn't numeric in addition (+) at -e line 1.
> 0
> SV = PVNV(0x803468) at 0x80021c
>   REFCNT = 1
>   FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
>   IV = 0
>   NV = 0
>   PV = 0x2017c0 "pie"\0
>   CUR = 3
>   LEN = 4
> SV = PVNV(0x803468) at 0x80021c
>   REFCNT = 1
>   FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
>   IV = 0
>   NV = 0
>   PV = 0x2017c0 "pie"\0
>   CUR = 3
>   LEN = 4
> 0
> SV = PVNV(0x803468) at 0x80021c
>   REFCNT = 1
>   FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
>   IV = 0
>   NV = 0
>   PV = 0x2017c0 "pie"\0
>   CUR = 3
>   LEN = 4
> 
> The implementation caches the numeric result, and uses that next time round,
> before it gets a chance to warn. It's been this way since (at least) 5.005_03
> (which is the oldest perl I have hanging around)
> 
> So is this a bug? Advice from Klortho #11943 suggests not:

Not a bug. I recall relying on this behaviour somewhere to speed up
loops. When you add 0 to a string value, it fills the IV/NV, so inside
the loop it doesn't have to convert.

>   #11943 Ah yes, and you are the first person to have noticed this bug since 1987.  Sure.
> 
> but is it right, or am I?

-- 
H.Merijn Brand         Amsterdam Perl Mongers (http://amsterdam.pm.org/)
using & porting perl 5.6.2, 5.8.x, 5.10.x  on HP-UX 10.20, 11.00, 11.11,
& 11.23, SuSE 10.1 & 10.2, AIX 5.2, and Cygwin.       http://qa.perl.org
http://mirrors.develooper.com/hpux/            http://www.test-smoke.org
                        http://www.goldmark.org/jeff/stupid-disclaimers/

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