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

I am learning the Perl code. I also want to use the beauty of the perl.

I can write like this

 $var or $var = 30;

But in this case how can I write the code using or

Or is there any smart way to write this

if (undef $var) { $x = 40; } else { $x = 50; }

Replies are listed 'Best First'.
Re: Need clean code
by 1nickt (Canon) on Mar 25, 2017 at 17:52 UTC

    You mentioned: $var or $var = 30;

    Better as:

    $var ||= 30;
    which assigns the value if the value of $var is not true. If your code allows $var to be defined but not true at that point (e.g. hold a value of '0'), then use:
    $var //= 30;
    which will only assign the value to $var if it is undefined.

    See perlop for more information.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Need clean code
by tybalt89 (Monsignor) on Mar 25, 2017 at 17:42 UTC
    $x = defined $var ? 50 : 40;

    I'm sure you meant 'defined' instead of 'undef'

Re: Need clean code
by davido (Cardinal) on Mar 25, 2017 at 19:54 UTC

    You are using the wrong or there. It will work often, except where it causes hard to find bugs. Instead, use || for "OR" expressions. Use or for control of flow, and || for expressions that evaluate to a value, as a general rule of thumb. The issue is the low precedence of or, which will trick you sometimes.

    So the first code snippet above could be written as:

    $var = $var || 30; # or $var ||= 30; # or $var = $var ? $var : 30; # or if you care about definedness rather than Boolean truth: $var = $var // 30; # or #var //= 30; # or $var = defined($var) ? $var : 30;

    Your second code snippet is also probably broken, because undef $var will set $var to undef and will then evaluate the expression's value, which is now always going to be undef. You probably meant this:

    if (! defined $var) { $x = 40; } else { $x = 50; }

    Which might be written more elegantly like this:

    $x = defined $var ? 50 : 40;

    For what it's worth, some of this applies equally in many languages such as C and C++. There's not really a concept of definedness in C/C++, but the concept of short circuiting in expressions using ||, and of ternary operators is nearly identical.


    Dave

      For what it's worth, some of this applies equally in many languages such as C and C++. There's not really a concept of definedness in C/C++, but the concept of short circuiting in expressions using ||, and of ternary operators is nearly identical.
      But please be warned that the semantics of the || and && operators in C/C++ is significantly different to Perl. For example, this Perl code:

      my $var; $var = 0; $var = $var || 30; print "1. var=$var\n"; $var = 0; $var ||= 30; print "2. var=$var\n";
      prints:
      1. var=30 2. var=30

      While this C++ code:

      int var = 0; var = var || 30; printf("1. var=%d\n", var); // var ||= 30; // oops, syntax error in C++
      prints:
      1. var=1
      Why? Because in C/C++, the (short-circuiting) || and && operators always return 0 or 1. Unlike Perl, they do NOT return the last value evaluated.

      IMHO, this deliberate Perl change in semantics of these two venerable short-circuiting operators was a clever innovation from Larry, making them more convenient to use, notably when setting default values.