in reply to •Perl Puzzle, was Re: (OT?) n(n-1)/2
in thread (OT?) n(n-1)/2

1., 2. see perlman strict, chapter 'strict vars'
as in a previuos thread already mentioned :-)
$a and $b arre exempted from the strict vars pragma due to their special role in the sort function
3. undef equals to '0'
4. dunno, didn't read it yet :-)

Have a nice day
All decision is left to your taste
  • Comment on Re: •Perl Puzzle, was Re: (OT?) n(n-1)/2

Replies are listed 'Best First'.
Re: Re: •Perl Puzzle, was Re: (OT?) n(n-1)/2
by Juerd (Abbot) on Mar 03, 2002 at 15:34 UTC

    3. undef equals to '0'

    No.
    undef evaluates to 0 in numeric context, to an empty string in string context. In boolean context, it is false. However, undef + 0 would eq '0', because first 0 + 0 is calculated, and then the result (0) is put in string context, and will be the string 0, which is in every way identical to 0 :)

    When undef is used with a numeric (+, -, * etcetera) or string operator (., x, =~, etc) that is not an assignment (+=, -=, ++, etc) itself, a warning will be emitted.

    ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
    Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
    -- vs lbh hfrq OFQ pnrfne ;)
        - Whreq
    

Re: Re: •Perl Puzzle, was Re: (OT?) n(n-1)/2
by dvergin (Monsignor) on Mar 03, 2002 at 15:01 UTC
    Read question 2 more carefully.

    Question 1 has to do with $a passing "use strict" for the reason you offer.

    But question 2 has to do with warnings on math with undef values. Although $a passes strict in this puzzle, it is still undef at the point we first encounter it.

    Just about any kind of math on such a value will not abort (as happens with the use strict violation referred to in Q. 1), but it normally does produce a warning (if warnings are turned on) e.g.:

    $a = $a + 1; # warns but executes anyway print $a; # 1
    In this case, the warning is: "Use of uninitialized value in addition (+) at testprog.pl line xx.". So question 2 is: Why is such a warning not produced in the snippet meryln refers to.