in reply to Re^3: 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?

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
  • Comment on Re^4: Summing the odd/even digits of a larger number (for upc check digits)...a better way?
  • Select or Download Code

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

    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'
Re^5: Summing the odd/even digits of a larger number (for upc check digits)...a better way?
by roboticus (Chancellor) on Jun 23, 2006 at 01:01 UTC
    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