Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Need clean code

by davido (Cardinal)
on Mar 25, 2017 at 19:54 UTC ( [id://1185951]=note: print w/replies, xml ) Need Help??


in reply to Need clean code

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

Replies are listed 'Best First'.
Re^2: Need clean code
by eyepopslikeamosquito (Archbishop) on Mar 25, 2017 at 23:17 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1185951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found