Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Stuck in Perl.. Partial code, but it needs more improvement.. Any suggestions, please?

by perlfan (Vicar)
on Jun 02, 2021 at 23:14 UTC ( #11133444=note: print w/replies, xml ) Need Help??


in reply to Re: Stuck in Perl.. Partial code, but it needs more improvement.. Any suggestions, please?
in thread Stuck in Perl.. Partial code, but it needs more improvement.. Any suggestions, please?

> ALWAYS CHECK THE RETURN FROM open ...

The typical idiom for this is,

open (my $fh, $mode, $file) || die $!;

This also mitigates the need for an -e check for read $mode that expect $file exist, particularly useful inside of an eval block or Try::Tiny construct; so that you can handle it.

Update, fixed precedence. Point remains the same.

  • Comment on Re^2: Stuck in Perl.. Partial code, but it needs more improvement.. Any suggestions, please?
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Stuck in Perl.. Partial code, but it needs more improvement.. Any suggestions, please?
by 1nickt (Abbot) on Jun 03, 2021 at 00:06 UTC

    Check your precedence. The code you showed does not do what you think.

    $ perl -Mstrict -wE 'say -e "fooble" ? 1 : 0; open my $fh, "<", "foobl +e" || die $!' 0
    $ perl -Mstrict -wE 'say -e "fooble" ? 1 : 0; open my $fh, "<", "foobl +e" or die $!' 0 No such file or directory at -e line 1.


    The way forward always starts with a minimal test.
      Thank you, fixed.
        I always use or in such situations, and I dislike perldocs which do otherwise, because these are invitations for refactoring problems.

        Personally I use || only in

        • in arithmetic expressions $x = $y || $z
        • to override false defaults were $x ||= $y

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        update

        swapped $a, $b with $y,$z to appease hippo ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2021-12-01 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (13 votes). Check out past polls.

    Notices?