Prev: FAQ 6.12 Can I use Perl regular expressions to match balanced text?
Next: FAQ 7.9 How do I create a module?
From: Uri Guttman on 18 Jan 2010 21:49 >>>>> "BM" == Ben Morrow <ben(a)morrow.me.uk> writes: BM> Quoth sreservoir <sreservoir(a)gmail.com>: >> On 1/18/2010 7:47 PM, Faith Greenwood wrote: >> > I have the following: >> > >> > use strict; >> > use warnings; >> > >> > my $number=0; >> > print "This:$number\n"; >> > print "That:$number\n" if $number; >> > >> > __END__ >> > >> > Of the two lines that print the variable, why does it print the first >> > line but not the second? >> > If I change the $number to equal 1, then both lines print fine. >> > >> > thank you >> >> because 0 is, traditionally, not true, even in perl where false is >> canonically undef. BM> No. False in perl is canonically a dualvar that is the empty string in BM> string context and 0 in numeric context (so there are no warnings on BM> numeric conversion, unlike a plain ""). i know that but the OP's issue was not knowing that 0 was false or not getting that if checked for false and not defined. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: sreservoir on 18 Jan 2010 21:59 On 1/18/2010 9:49 PM, Uri Guttman wrote: >>>>>> "BM" == Ben Morrow<ben(a)morrow.me.uk> writes: > > BM> Quoth sreservoir<sreservoir(a)gmail.com>: > >> On 1/18/2010 7:47 PM, Faith Greenwood wrote: > >> > I have the following: > >> > > >> > use strict; > >> > use warnings; > >> > > >> > my $number=0; > >> > print "This:$number\n"; > >> > print "That:$number\n" if $number; > >> > > >> > __END__ > >> > > >> > Of the two lines that print the variable, why does it print the first > >> > line but not the second? > >> > If I change the $number to equal 1, then both lines print fine. > >> > > >> > thank you > >> > >> because 0 is, traditionally, not true, even in perl where false is > >> canonically undef. > > BM> No. False in perl is canonically a dualvar that is the empty string in > BM> string context and 0 in numeric context (so there are no warnings on > BM> numeric conversion, unlike a plain ""). > > i know that but the OP's issue was not knowing that 0 was false or not > getting that if checked for false and not defined. > the problem with this group is that even when someone agrees with you, they will disagree with your example. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: Ben Morrow on 18 Jan 2010 21:58 Quoth sreservoir <sreservoir(a)gmail.com>: > On 1/18/2010 9:40 PM, sreservoir wrote: > > On 1/18/2010 9:05 PM, Ben Morrow wrote: > >> > >> No. False in perl is canonically a dualvar that is the empty string in > >> string context and 0 in numeric context (so there are no warnings on > >> numeric conversion, unlike a plain ""). > >> > >> ~% perl -E'say defined !1' > >> 1 > >> ~% perl -wE'say 0 + !1' > >> 0 > >> ~% perl -wE'say "[", ("" . !1), "]"' > >> [] > >> ~% perl -wE'say 0 + ""' > >> Argument "" isn't numeric in addition (+) at -e line 1. > >> 0 > >> ~% > >> > >> True is canonically the string "1". > > > > ~% perl -wle'print !1' > > Use of uninitialized value in print at -e line 1. > > > > ~% > > > > I suspect this is a historical thing that I never remembered. > > a newer perl returns the same results as your perl. Which version of perl was your original test? I get !1 defined for all versions >5.6.0 (the oldest perl I have lying around). Ben
From: Ben Morrow on 18 Jan 2010 22:06 Quoth sreservoir <sreservoir(a)gmail.com>: > On 1/18/2010 9:28 PM, J�rgen Exner wrote: > > sreservoir<sreservoir(a)gmail.com> wrote: > >> because 0 is, traditionally, not true, even in perl where false is > >> canonically undef. > > > > That is not correct. While the boolean values of undef is indeed false, > > this is by no means canonical. There are several other scalar values, > > the numerical 0 just being one of them. Please see 'perldoc perldata' > > for details. > > huh. historically, the comparisons returned undef for false. well, I > guess you learn something new every day. I don't know what you mean by 'historically'. sv_no (the canonical false value) has been as I describe since 5.000; 4.036 had str_no, which appears to be simply the empty string (I guess Perl 4 didn't have numeric conversion warnings?). Ben
From: Ben Morrow on 18 Jan 2010 22:12
Quoth "Uri Guttman" <uri(a)StemSystems.com>: > >>>>> "BM" == Ben Morrow <ben(a)morrow.me.uk> writes: > > BM> Quoth sreservoir <sreservoir(a)gmail.com>: > >> > >> because 0 is, traditionally, not true, even in perl where false is > >> canonically undef. > > BM> No. False in perl is canonically a dualvar that is the empty string in > BM> string context and 0 in numeric context (so there are no warnings on > BM> numeric conversion, unlike a plain ""). > > i know that but the OP's issue was not knowing that 0 was false or not > getting that if checked for false and not defined. I know you know that, which is why I wasn't saying it to you. I was saying it to sreservoir, who apparently didn't. Ben |