Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Passing variable to if statement

by PerlSufi (Friar)
on Jul 16, 2014 at 20:19 UTC ( [id://1093935]=note: print w/replies, xml ) Need Help??


in reply to Passing variable to if statement

In addition to what AppleFritter said, use the best practice way of file handling:
use strict; use warnings; use autodie; my $file = 'array.txt'; open(my $fh, '<', $file);

Replies are listed 'Best First'.
Re^2: Passing variable to if statement
by Jim (Curate) on Jul 16, 2014 at 23:00 UTC

    Another Perl best practice:  Always decode text upon input and encode text upon output—and always to do it explicitly. Don't leave the handling of the character encoding of the text to whatever your version of perl in your environment (locale) defaults to.

    use strict; use warnings; use autodie qw( open close ); binmode STDOUT, ':encoding(UTF-8)'; # Or ASCII, Latin-1, etc. my $file = 'array.txt'; open my $fh, '<:encoding(UTF-8)', $file; # ... close $fh;

    Better…

    use strict; use warnings; use autodie qw( open close ); use open qw( :encoding(UTF-8) :std ); # Or ASCII, Latin-1, etc. my $file = 'array.txt'; open my $fh, '<', $file; # ... close $fh;
Re^2: Passing variable to if statement
by Anonymous Monk on Jul 16, 2014 at 20:55 UTC

    I think OP's original code is fine in this regard. I'd say autodie is helpful for those who know all the things that it does, or e.g. for dropping into legacy scripts that don't do enough error checking. But, as I've discovered, it can also cause problems when it's used, and its bug trackers (1, 2) show I'm not the only one. Don't get me wrong, it's a good recommendation, I just disagree that it's a "best practice" - it's just MTOWTDI.

      I think OP's original code is fine in this regard.

      Whoops, I missed the missing or die in the second open. Nevermind that sentence.

        Agreed, I didn't really mean  use autodie; was a best practice.. should have clarified and used the  or die instead :p
Re^2: Passing variable to if statement
by PeachCityDude (Novice) on Jul 16, 2014 at 20:27 UTC
    Thank you for the tip on file handling.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found