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

bug or not? constants warn only once

Thread Next
From:
Nicholas Clark
Date:
April 24, 2008 09:36
Subject:
bug or not? constants warn only once
Message ID:
20080424163621.GH79799@plum.flirble.org
$ 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:

  #11943 Ah yes, and you are the first person to have noticed this bug since 1987.  Sure.

but is it right, or am I?

Nicholas Clark

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