Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: "open" Best Practices

by stevieb (Abbot)
on Jul 11, 2019 at 14:53 UTC ( #11102687=note: print w/replies, xml ) Need Help??


in reply to "open" Best Practices

Nice! I'd add a blurb about using close(), as well as a scoped block example (perhaps linking the two). One good example of when to use a block is using a local record separator variable (the close isn't really necessary in this case, but I digress):

my $file = 'test.txt'; my $json; { local $/; open my $fh, '<', $file or die $!; $json = <$fh>; close $fh; }

Replies are listed 'Best First'.
Re^2: "open" Best Practices
by haukex (Chancellor) on Jul 11, 2019 at 15:02 UTC

    Thanks, and yes, good points! The node is aimed at people using the open statement in a "non-best-practice" way, so I'd like to keep the node short enough so that they might still read it (hence all the bold text and TL;DR at the top), and I'd like to keep the focus on the open statement itself - I left out a discussion e.g. of piped opens and checking close for errors and such intentionally, because when answering questions about that, I have other nodes I'd link to instead. Perhaps your reply to my node pointing these things out is enough? :-)

    Update: Ok, I've updated the root node!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2019-07-19 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?