Normally one should check for success when doing file operations such as
open. However, I wonder if there is a point to doing this if you are
open'ing a filehandle against data held in a scalar reference. (I noticed that
GrandFather didn't in
this post.) If the script has compiled and is executing, in what way could the
open fail? In this (slightly silly) code example
use strict;
use warnings;
open my $inFH, q{<}, \ <<END or die qq{open: $!\n};
The boy stood on the burning deck,
Eating an ice cream, Walls.
The ice fell down his trouser leg,
And paralysed his left kneecap.
END
while ( <$inFH> )
{
print qq{$.: $_} if m{([aeiou])\1};
}
close $inFH or die qq{close: $!\n};
output,
1: The boy stood on the burning deck,
4: And paralysed his left kneecap.
my feeling is that checking for success on open and close are not needed and could safely be dispensed with. Is this a dangerous assumption?
Cheers,
JohnGG
Update: Thank you, jbert, kyle and Sartan for your responses. As happens, I am very likely to continue checking for success as it is already an ingrained habit and I wasn't even aware that one could open a scalar until recently. jbert, I thought your point regarding possible future implementations was particularly telling.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.