Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Print on closed file handle

Reply
Thread Tools

Print on closed file handle

 
 
tonyshirt@hotmail.com
Guest
Posts: n/a
 
      09-28-2005
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?

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
news:(E-Mail Removed) ups.com:

> 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

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
tonyshirt@hotmail.com
Guest
Posts: n/a
 
      09-29-2005

A. Sinan Unur wrote:
> (E-Mail Removed) wrote in
> news:(E-Mail Removed) ups.com:
>
> > 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


Thanks for the advice!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM
Opera - .closed not accessible if window is closed? Matt Kruse Javascript 5 09-09-2003 01:27 AM



Advertisments