in reply to Dangerous Names

"Nan McEntire" get numified to NaN on some systems, and the NaN check apparently causes <=> to return undef.

$ perl -MDevel::Peek -e'Dump(0 <=> "Nan McEntire")' SV = NULL(0x0) at 0x503848 REFCNT = 2147483621 FLAGS = (PADBUSY,PADTMP,READONLY)

Replies are listed 'Best First'.
Re^2: Dangerous Names
by samtregar (Abbot) on Dec 17, 2008 at 22:07 UTC
    Right, that's half the battle. Furthermore, sort barfs on undefs, which I find very odd. Pretty much everything else in Perl is happy to treat undef as 0, why not sort?

    -sam

      Pretty much everything else in Perl is happy to treat undef as 0, why not sort?
      I think you're confused
      D:\>perl -wle"print undef" Use of uninitialized value in print at -e line 1. D:\>perl -wle"warn undef" Use of uninitialized value in warn at -e line 1.
      Nothing in perl treats undef as 0 without barfing
      D:\>perl -we"print 1 <=> undef Use of uninitialized value in numeric comparison (<=>) at -e line 1. 1 D:\>perl -we"print 1 + undef Use of uninitialized value in addition (+) at -e line 1. 1 D:\>perl -we"print 1 - undef Use of uninitialized value in subtraction (-) at -e line 1. 1 D:\>
        >> Pretty much everything else in Perl is happy to treat undef as 0, why not sort?
        > I think you're confused
        [...]
        These are just warnings. sort dies, that's the difference. So if you don't have warnings on, you can do string comparisons with <=> as long as you want, and suddenly with a special input your script will die instead of only warn.
        I see it as another encouragement to always use warnings =)