Print on closed file handle

T

tonyshirt

I'm using Active state 5.8 and writing for windows. I have a very
simple parser that writes a csv file (Comma separated Values). It
looks like this:

eval{
open APOUT, ">APresults.csv" || die "Can't open file
APresults.csv!";
foreach my $seqID (keys %hashRef){

if (grep {$_ eq $seqID} @SNPlower){

print APOUT
"$hashRef{$seqID}{PNLNUM},$seqID,$hashRef{$seqID}{SBEStrand},$hashRef{$seqID}{SBE},$hashRef{$seqID}{WARN}";
print APOUT "\n";
}

}
close APOUT;
};

if ($@){
print $@;
}

I noticed when I accidentally had the file APresults.csv open I get
this:

print() on closed filehandle APOUT at APResults parse.pl line 101, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 102, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 101, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 102, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 101, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 102, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 101, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 102, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 101, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 102, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 101, <>
line 230.
print() on closed filehandle APOUT at APResults parse.pl line 102, <>
line 230.

which is exactly how many times I call the print statement in the loop.
My question is if the file can't be opened because its busy with
another process, shouldn't the open just die?
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I'm using Active state 5.8 and writing for windows. I have a very
simple parser that writes a csv file (Comma separated Values). It
looks like this:

eval{
open APOUT, ">APresults.csv" || die "Can't open file
APresults.csv!";

You should include the reason why the file could not be opened in the
error message. Also, you have used || in the call above. It will never
die.

open my $apout, '>', 'APresults.csv'
or die "Can't open 'APresults.csv': $!";

If you want to use ||, then you should properly paranthesize the open
call:

open(my $apout, '>', 'APresults.csv')
|| die "Can't open 'APresults.csv': $!";

....
if ($@){
print $@;
}

IMNSHO, it is better to write error messages to stderr. In fact, given
your script, I would just use:

die $@ if $@;
....

Sinan
 
T

tonyshirt

A. Sinan Unur said:
(e-mail address removed) wrote in


You should include the reason why the file could not be opened in the
error message. Also, you have used || in the call above. It will never
die.

open my $apout, '>', 'APresults.csv'
or die "Can't open 'APresults.csv': $!";

If you want to use ||, then you should properly paranthesize the open
call:

open(my $apout, '>', 'APresults.csv')
|| die "Can't open 'APresults.csv': $!";

...


IMNSHO, it is better to write error messages to stderr. In fact, given
your script, I would just use:

die $@ if $@;
...

Sinan

Thanks for the advice!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top