in reply to Re: How to Do Multiple OR in For Loops
in thread How to Do Multiple OR in For Loops

Note though that || short circuits (doesn't evaluate more clauses than needed to calculate the answer), where as grep will always perform all checks.

Of course, it's not very common to have so many clauses that it matters. You may need hundreds, if not thousands of clauses to make a significant difference (and then only if there's a true clause "early" enough).

Replies are listed 'Best First'.
Re^3: How to Do Multiple OR in For Loops
by LanX (Saint) on Apr 13, 2011 at 11:04 UTC
    I was wondering if the short circuit could cause side effects. But in this case there is no risk of auto-vivication.

    With side-effects I'd rather prefer a non short circuit solution for clarity.

    If a short circuit effect is side effects areš wanted, it should be done explicitly.

    Cheers Rolf

    1) corrected bad phrasing!

      If a short circuit effect is wanted, it should be done explicitly.
      That appears to be saying "if you want the short circuit effects of || and &&, you shouldn't use || and &&, but do it explicitly, but it's ok to use || and && (and get the short circuiting) if you don't want it".

      But that would be strange, and it's probably not what you wanted to communicate.

      Personally, I very much prefer to write things like:

      if (ref $code && $code->()) {...} # short circuit
      instead of
      if (ref $code) { if ($code->()) { # explicitly ... } }
      It's a long time since I coded Pascal, and I don't miss much of it. Actually, I don't think I miss anything from it.
        > That appears to be saying "if you want the short circuit effects ...

        > it's probably not what you wanted to communicate.

        Sorry my fault!

        I meant if side effects are wanted they shouldn't be limited to elements till short circuit.

        e.g. if you want auto-vivication do it for all of them.

        Corrected in the -2 post.

        Cheers Rolf