SPUG: "Boolean" return values
Michael R. Wolf
MichaelRWolf at att.net
Tue Oct 14 00:21:15 PDT 2008
Colin Meyer wrote:
> On Mon, Oct 13, 2008 at 03:03:26PM -0700, Michael R. Wolf wrote:
>> Does anyone know the motivation for the return values from the boolean
>> operators?
>
> We have boolean context, but not boolean operators. Maybe "!". Most of
> the logical operators have return values based on their input.
>
>> true returns (numeric) 1
>> false returns (string) ''
>>
>> Why not just 0 and 1? Why the mix of numeric and string?
>
> They return a multi-valued scalar:
>
> > perl -MDevel::Peek -le'Dump( ! "" ); Dump( ! 0 );'
> SV = PVNV(0x605060) at 0x2af2764040d0
> REFCNT = 2147483647
> FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
> IV = 1
> NV = 1
> PV = 0x606030 "1"\0
> CUR = 1
> LEN = 8
> SV = PVNV(0x605060) at 0x2af2764040d0
> REFCNT = 2147483647
> FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
> IV = 1
> NV = 1
> PV = 0x606030 "1"\0
> CUR = 1
> LEN = 8
>
> In a string context, that will be "1"; Integer or real: 1; Boolean: true;
Thanks for the reply. It helped me reformulate my question (even if I
don't understand all nuances of the underlying C code as reported by
Devel::Peek).
Boolean (err, 'logical') operator return values, Take 2:
The 6 relational operators (both the stringy and numy flavors), and
various other logical operators seem to return reasonable values in the
IV and NV slots (specifically 1 for "true" and 0 for "false"). I don't
understand why the PV slot is "1" for true and "" for false. Why not
"1" and "0" so that the stringy value corresponds to the numy value?
It's especially confusing to folks (me included) who write code like this:
$true = 1 < 100;
$false = 100 < 1;
print "true is '$true' and false is '$false'\n";
It seems non-parallel, or imbalanced. Can you reframe it for me in a
way that makes sense, creates balance, and seems consistent (for some
definition of consistent)?
To wit....
use Devel::Peek;
# Here's what "true" looks like...
Dump(! "");
Dump(! 0);
Dump(1 < 100);
# Here's what "false" looks like...
Dump(!! "");
Dump(!! 0);
Dump(100 < 1);
SV = PVNV(0x10022820) at 0x10012a08
REFCNT = 2147483644
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x10022008 "1"\0
CUR = 1
LEN = 4
SV = PVNV(0x10022c40) at 0x1004fbc0
REFCNT = 1
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x100220f8 "1"\0
CUR = 1
LEN = 4
SV = PVNV(0x10022c70) at 0x1004fcc8
REFCNT = 1
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x10022218 "1"\0
CUR = 1
LEN = 4
SV = PVNV(0x10022808) at 0x100129f0
REFCNT = 2147483645
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x10022004 ""\0
CUR = 0
LEN = 4
SV = PVNV(0x10022c88) at 0x1004fc08
REFCNT = 1
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x1002221c ""\0
CUR = 0
LEN = 4
SV = PVNV(0x10022ca0) at 0x1004fb48
REFCNT = 1
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x10022220 ""\0
CUR = 0
LEN = 4
More information about the spug-list
mailing list