From: Chas. Owens
Sent: Friday, July 07, 2017 12:34 AM
To: hw ; beginners@perl.org
Subject: Re: perl -e 'my $i = 0; $i = defined($i) ? (!!$i) : 0; print "i: $i\n";'
On Thu, Jul 6, 2017 at 9:38 AM hw <h...@gc-24.de> wrote:
Chas. Owens wrote:

$i started off as an IV, but gets promoted to a PVIV by being used in string context.

I find it a little surprising that use of the '!' operator is all that's needed to add the stringification stuff:

C:\>perl -MDevel::Peek -le "$i=0; Dump($i); Dump(!$i);"
SV = IV(0x93ece0) at 0x93ece4
 REFCNT = 1
 FLAGS = (IOK,pIOK)
 IV = 0
SV = PVNV(0xb0a934) at 0xb03090
 REFCNT = 2147483644
 FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
 IV = 1
 NV = 1
 PV = 0xb0330c "1"\0
 CUR = 1
 LEN = 12

If the '!' operator didn't do that, then I believe the OP would be seeing precisely what he expects.

So ... why should the '!' operator *not* respect the string/numeric context of the operand ? Why does it insist on logically negating *both* the numeric and string slots of the variable, even when those slots are initially unset ?

Maybe it's just that that's the way it has always been done - or maybe there's a more convincing reason ?

Cheers,
Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to