in reply to Re^2: Summing the odd/even digits of a larger number (for upc check digits)...a better way?
in thread Summing the odd/even digits of a larger number (for upc check digits)...a better way?

GrandFather:

OK ... I thought that since the return didn't get to return the string that the function would return undef. So I shouldn't've removed the "return undef;" at the end.

So what is returned, then? I'm suspecting that it's the value of the condition at the end (i.e. false), but I await edification! ;^)

--roboticus

  • Comment on Re^3: Summing the odd/even digits of a larger number (for upc check digits)...a better way?

Replies are listed 'Best First'.
Re^4: Summing the odd/even digits of a larger number (for upc check digits)...a better way?
by GrandFather (Saint) on Jun 23, 2006 at 00:39 UTC

    That's what I expect too. The following seems to validate that view.

    use warnings; use strict; use Data::Dump::Streamer; my $condRes = 1 == 1; print '1 == 1 '; Dump ($condRes); $condRes = 1 == 0; print '1 == 0 '; Dump ($condRes); $condRes = doCond (1); print 'doCond (1) '; Dump ($condRes); $condRes = doCond (0); print 'doCond (0) '; Dump ($condRes); sub doCond { 1 == shift; }

    Prints:

    1 == 1 $VAR1 = 1; 1 == 0 $VAR1 = ''; doCond (1) $VAR1 = 1; doCond (0) $VAR1 = '';

    DWIM is Perl's answer to Gödel

      You are right that undef is not being returned. False is being returned. False is a dual var. It's 0 when used in numerical context, and '' when used in string context. That's not quite the same as '', as the following shows:

      use warnings; my $false = !1; my $nulstr = ''; print("'$false'\n"); # '' print("'$nulstr'\n"); # '' print(0+$false, "\n"); # 0 print(0+$nulstr, "\n"); # 0 with 'Argument "" isn't numeric'
      I'm convinced. I just tried out your example, and added a case because I wasn't certain that evaluating a conditional would yield the same as a conditional return. Then, as I've never used Data::Dump::Streamer I also added a case with an explicit undef, just to be certain...

      $condRes = condReturn (0); print 'condReturn (0) '; Dump ($condRes); $condRes = condReturn (1); print 'condReturn (1) '; Dump ($condRes); sub condReturn { return "Foobar" if shift == 1; } $condRes = mebbeUndef (0); print 'mebbeUndef (0) '; Dump ($condRes); $condRes = mebbeUndef (1); print 'mebbeUndef (1) '; Dump ($condRes); sub mebbeUndef { return undef if shift == 0; }
      ...and got the expected:

      condReturn (0) $VAR1 = ''; condReturn (1) $VAR1 = 'Foobar'; mebbeUndef (0) $VAR1 = undef; mebbeUndef (1) $VAR1 = '';
      --roboticus