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 25, 2008 03:05
Subject:
Re: bug or not? constants warn only once
Message ID:
20080425120521.0fe40ec9@pc09.procura.nl
On Fri, 25 Apr 2008 09:37:50 +0200, Bram <p5p@perl.wizbit.be> wrote:

> (Quote from previous message)
> > 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.
> 
> Do you recall how exactly you used it?

$hash{$_} += 0 for @num_keys;

or in XS or with Scalar::Util

$ perl -MScalar::Util=dualvar -MDevel::Peek -wle'Dump dualvar (42, "Magic")'
SV = PVNV(0x74d888) at 0x720210
  REFCNT = 1
  FLAGS = (TEMP,IOK,POK,pIOK,pPOK)
  IV = 42
  NV = 0
  PV = 0x736dc0 "Magic"\0
  CUR = 5
  LEN = 8

> IMHO:
> $foo = "abc";
> $foo += 0;
> $foo + 3;
> ==> This should warn once.

Yes

> $foo= "abc";
> $bar = $foo + 0;
> $baz = $foo + 3;
> ==> This should warn twice.

Probably, yes

> > Also think dualvar. I use that, and I'd hate all my error messages to
> > suddenly start warning in numeric context.
> 
> The way I see it with my - very, very - limited knowledge of the Perl  
> internals:
> If the numeric value is converted from the PV slot of an SV and  
> contains other then numeric data ("pie", "12ab", ...)  then it should  
> do what it does now but not set IOK flag.

I have no idea how much would break if you do that

> For dualvars this wouldn't cause problems since there the IOK flag is  
> set (which means the PV is never looked at).

That's what I meant

> In some situations it could be useful that it warns multiple times in  
> other it might be annoying... (but there are problems to solve the  
> annoying onces - adding += 0)
> 
> Consider a daemon for example... at the start of the code a scalar is  
> set to a string instead of an int.
> Assume that the scalar is used in a numeric context in the main loop  
> of the daemon.
> The first time it happens it will warns, the second time it will  
> silently use 0. This could make it easy to miss the warning (and the  
> problems it create).

-- 
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