peterr has asked for the wisdom of the Perl Monks concerning the following question:

I'm wanting to fully understand what the short circuit || means in the code below. I thought it was a short circuit for OR

my $ct = $part->contentType || 'text/plain'; $ct eq 'text/plain' || $ct eq 'text/html' or next; ..some code

Replies are listed 'Best First'.
Re: What does the short circuit || mean ?
by choroba (Cardinal) on Mar 10, 2015 at 00:57 UTC
    On the first line, || evaluates the left side first. If it's true, it gets assigned to $ct, otherwise, $ct becomes text/plain.

    On line 2, you can add parentheses as follows:

    (($ct eq 'text/plain') || ($ct eq 'text/html')) or next;

    which makes it equivalent to

    next if $ct ne 'text/plain' and $ct ne 'text/html';

    The "short circuiting" is most noticable in the or operator: next is evaluated only if the previous condition is false.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks choroba for explaining that.

      It's like saying if $part->contentType is undefined, $ct will default to a value of "text/plain"

        ... if $part->contentType is undefined ...

        This is a little tricky, but to be precise, you're saying "if $part->contentType is false", then do something. In Perl, falsity is represented by any of: undef (the undefined value), "" (the empty string), 0 (numeric zero), or '0' (a string consisting of a single 0 character). Use the  // operator to test specifically for defined-ness.

        Update: See also True and False (aka Booleans).


        Give a man a fish:  <%-(-(-(-<

Re: What does the short circuit || mean ?
by LanX (Saint) on Mar 10, 2015 at 00:57 UTC
    || vs or is a matter of precedence.

    update from perlop

    Logical or and Exclusive Or

    Binary "or" returns the logical disjunction of the two surrounding expressions. It's equivalent to || except for the very low precedence. This makes it useful for control flow:

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

      Thanks Rolf. :)
Re: What does the short circuit || mean ?
by locked_user sundialsvc4 (Abbot) on Mar 10, 2015 at 13:23 UTC

    It is for all of these reasons (clarity, “falseness” gotchas) that I strongly prefer to see the plain-and-simple use of if or unless ... and, explicit relational operators.   If you are intending to make a decision or a branch here ... say that.   If you intend to test for a certain thing, say what you are intending to test for.   (And throw an exception if when “none of the above” actually happens.)

    The computer will never mis-read your source code:   it will parse it, one way or another, and then it will do what it thinks you said.   But, people mis-read source code all the time, and that’s where a lot of head-banging (and, costly) project delays come from.

Re: What does the short circuit || mean ?
by v-zor (Initiate) on Mar 11, 2015 at 11:49 UTC
    It's called lazy evaluation. You either assign the contentType if it is non-nil else you assign the 'text/plain' mime header. The second clause checks for a html or text mime-type or else continues the loop with next.