in reply to Re: How to return a two dimensional array from a function in Perl?
in thread How to return a two dimensional array from a function in Perl?

and and or are designed to be used to join statements. Use && and || inside of expressions.

Huh? What practical difference is there between this:

if (($graph[$i][$j] == 0) and ($i != $j))
and this:
if (($graph[$i][$j] == 0) && ($i != $j))
apart from the fact the former is more English-like? Isn't each condition around the conjunction a kind of statement? (What exactly is the difference between a "statement" and an "expression"? -- actually, I'm not sure I want to get into that...)

Replies are listed 'Best First'.
Re^3: How to return a two dimensional array from a function in Perl?
by ikegami (Patriarch) on Nov 03, 2008 at 04:44 UTC

    What practical difference is there between this:

    One practice is more likely to eventually burn you than the other.

    What exactly is the difference between a "statement" and an "expression"?

    I suppose it'd be safer to look at whether the program's flow is being controlled or not. or next, or return, or die, etc.

      One practice is more likely to eventually burn you than the other.

      Please correct me if I'm wrong: The important point about choosing between "and/or" vs. "&&/||" involves their differences in precedence relative to other operators (especially the "comma operator"); the classic example (often seen in SoPW questions) being:

      open $fh, "<", $filename || die "oops!"; # should use "or" here # (or else put parens around the args for the " +open()" call)
      But the "burn" only happens when you use  &&/|| in a context where  and/or would be needed to do the right thing. In contrast, I can't think of any context appropriate for  &&/|| such that  and/or wouldn't serve equally well.

      If you can find a context where  &&/|| produce an intended result and replacing them with  and/or produces a different (incorrect) result, that would be remarkably informative (and surprising, I think). NB: No fair making up obfu examples that depend on l-to-r vs. r-to-l differences -- people who get into that kind of trouble need other kinds of help and advice.

      I'm guessing that BrowserUK has a similar view, and the reason we're both giving you grief about this is that you seem to be suggesting there are situations where using  and/or should be disfavored or unadvisable, whereas B and I would rather say that the better advice is "when in doubt, use  and/or" (which is what the OP seemed to be doing in the first place).

        If you can find a context where &&/|| produce an intended result and replacing them with and/or produces a different (incorrect) result, that would be remarkably informative

        Easy. Consider rewriting

        if ($a == 1 or $b == 2) { ... }

        so that a variable is used to hold the condition.

        my $cond = $a == 1 or $b == 2; XXX if ($cond) { ... }

        Why write the same expression two different ways depending on context?