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: J�rgen Exner on 18 Jan 2010 21:22 Faith Greenwood <fgnowfg(a)gmail.com> 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? Maybe because you explicitely tell Perl to print the second line only if $number is true? >If I change the $number to equal 1, then both lines print fine. Big surprise, 1 is a true value, 0 is not. jue
From: J�rgen Exner on 18 Jan 2010 21:28 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. jue
From: sreservoir on 18 Jan 2010 21:40 On 1/18/2010 9:05 PM, Ben Morrow wrote: > > 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. > > 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. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: sreservoir on 18 Jan 2010 21:42 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. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: sreservoir on 18 Jan 2010 21:44
On 1/18/2010 9:40 PM, sreservoir wrote: > On 1/18/2010 9:05 PM, Ben Morrow wrote: >> >> 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. >> >> 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. false as a string never struck me as useful though. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe" |