SPUG: "Boolean" return values

Michael R. Wolf MichaelRWolf at att.net
Wed Oct 15 18:34:08 PDT 2008

Colin Meyer wrote:

> On Tue, Oct 14, 2008 at 12:21:15AM -0700, Michael R. Wolf wrote:
>> 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)?
> I guess that it makes sense to me. In regular code, I don't have the
> desire to print out the values of $true and $false. I'd be more inclined
> to use them in a boolean context, and write something like:
>     print 'Your number is ',
>         ( $num < 100 ? 'less' : 'greater' ),
>         ' than 100';

I'm not likely to print them to normal users, but I've found that while 
debugging, it's awkward to have to use a conditional operator (as above) 
or some other magic just to see them.

my $true = 1 < 100;
my $false = 100 < 1;

print "true is '$true' and false is '$false'\n";
print "\n";

foreach my $answer ($true, $false) {
     print "    raw answer is '$answer'\n";
     print "coerced answer is '@{[$answer+0]}'\n";
     print "\n";

I'm so used to Perl's DWIM feature providing me with the freedom to 
SWIM.  Printing return values from logical operators seems to be cutting 
across the normal DWIM/SWIM that has me working *with* the force instead 
of across the grain.  I know how to make Perl bend to my will, but I'm 
wondering if there's some higher intelligence that I'm missing so that I 
can change my mind to fit the deeper insight.

Anyone else have a good guess (or even a SWAG) as to why stringy logical 
values (PV's) are *not* the stringification of the integer and double 
(IV's and NV's) for the 'false' case, but *are* for the 'true' case?

More information about the spug-list mailing list