Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: "open" Best Practices

by haukex (Archbishop)
on Jul 13, 2019 at 06:11 UTC ( [id://11102772]=note: print w/replies, xml ) Need Help??


in reply to Re: "open" Best Practices
in thread "open" Best Practices

Well, I disagree with the "never", but I did strengthen my warning against it a bit, thanks! BTW, some of those links are of course very relevant, but some of them (e.g. the first two) don't mention the || vs or at all.

Replies are listed 'Best First'.
Re^3: "open" Best Practices
by eyepopslikeamosquito (Archbishop) on Jul 13, 2019 at 09:04 UTC

    I always use the low precedence and and or operators for flow of control, for example preferring:

    open(my $fh, '<', $file) or die "error opening '$file': $!";
    to the equivalent:
    open(my $fh, '<', $file) || die "error opening '$file': $!";

    while always using && and || inside logical expressions, for example preferring:

    if ($x > 5 || $y < 10) ...
    to the equivalent:
    if ($x > 5 or $y < 10) ...

    When this style is followed consistently, I find the code easier to read and understand at a glance.

    See also Perl Best Practices, Chapter 4, "Values and Expressions", "Don't mix high- and low-precedence booleans" item.

      I agree with everything you've said, and use the operators in the same way myself, with very few exceptions (for example, I might sometimes write if (not ...) instead of unless(...) if it reads better). Consistency is definitely a good thing!

      I was just disagreeing with the "never" part - TIMTOWTDI, and function() || ... is valid Perl and sometimes applicable. In the context of the root node, I explicitly included that form because a newcomer might have seen it in other code, and I think it's good to say that it's not wrong, but also not recommended. Anyway, I've edited the root node further to highlight that even more.

      (What do you think about if( open($fh, '<', $foo) || open($fh, '<', $bar) )? ;-) )

        What do you think about if( open($fh, '<', $foo) || open($fh, '<', $bar)

        Dogfood time? ;-)

        for my $infile ($foo, $bar) { last if open ($fh, '<', $infile); warn "Cannot open '$infile' for reading: $!"; } unless (eof $fh) { ... } else { warn "None of the files could be opened. What a shame."; }

        I take your point though and there it's arguable whether on not it's control flow. I would not object to either || or or in such a situation.

        eyepopslikeamosquito open() ... I was just disagreeing with the "never" part - TIMTOWTDI, ... is valid Perl

        Hi, Obfuscated Code is valid perl, but its obvioulsy not employable style.

        Using open()|| is less obvious, but still a shibboleth

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-28 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found