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

Re: Any downsides to this slurp idiom?

by Eily (Monsignor)
on Jun 22, 2018 at 12:54 UTC ( #1217187=note: print w/replies, xml ) Need Help??

in reply to Any downsides to this slurp idiom?

One downside I see is that it's not easy to read. You're more likely to read

open $fh, '<', $file or die $!; process($fh) if is_valid($fh);
die $! unless open $fh, '<', $file; is_valid($fh) and process($fh);
because the first version puts the important information first. In that idiom, the important part (the assignment) is torn away to both ends.

Also if you have something like this:

my $data; { local $/; open my $fh, '<', $file or die $!; $data = <$fh>; }
You can afford not to know what $/, $! and '<' mean, and still have a pretty good idea that a file is being opened, and the content written to $data (even if you don't understand exactly why it's written like that). In your idiom, you have one line with many harder concepts, and you can't really extract the easy part.

I'm tempted to say the best solution would be to use a function that abstracts all that away, but I honestly I never do it myself :P.

it's bad style to rely on the implicit close
Personally I even consider it good style, because if you actively rely on the implicit close, you are going to focus on having the correct scope for your handle. The alternative is either you close before the end of the scope, or rewriting the variable, in which case the close doesn't do anything more, or you close the handle and keep it, which is only fine if you actually meant, and needed it to do it.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2023-03-26 09:10 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.