in reply to order of operations with conditionals at the end

if is not an operator. It's part of the syntax of an if statement. An if statement evaluates its condition first, then evaluates the rest if and only if the condition evaluated to something true.

EXPR if CONDEXPR;

is exactly the same as

(CONDEXPR) and EXPR;

By the way,

my $var ... if ...;

is not allowed. (Same goes for unless.) You should be using

my $message; $message = "Congrats" if defined $reward;

or

my $message = $reward ? "Congrats" : undef;

Replies are listed 'Best First'.
Re^2: order of operations with conditionals at the end
by JavaFan (Canon) on Mar 26, 2012 at 23:34 UTC
    Not allowed?
    $ perl5.15.9 -wE 'my $x = 1 if 2; say $x // "???"' 1 $ perl5.15.9 -wE 'my $x = 1 if 0; say $x // "???"' ??? $
    I won't recommend it, but it is still allowed. I thought it started warning under certain conditions since 5.14, but I'm not able to reproduce it.
      The compiler does compile it, but it has been explicitly forbidden in the docs, and it has been so for far far longer than 5.14. (I'm guessing 5.6 or 5.8.)
        Hmmm, the closest I could find is:
        B<NOTE:> The behaviour of a C<my>, C<state>, or C<our> modified with a statement modifier conditional or loop construct (for example, C<my $x if ...>) is B<undefined>. The value of the C<my> variable may be C<undef>, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
        "Undefined behaviour", "may change in the future", "don't rely on it", and "here be dragons" are all hints not to use it. But it doesn't add up to "explicitly forbidden in the docs" in my book.