Print on closed file handle

Discussion in 'Perl Misc' started by tonyshirt@hotmail.com, Sep 29, 2005.

  1. Guest

    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?
     
    , Sep 29, 2005
    #1
    1. Advertising

  2. wrote in
    news::

    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 29, 2005
    #2
    1. Advertising

  3. Guest

    A. Sinan Unur wrote:
    > wrote in
    > news::
    >
    > > 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!
     
    , Sep 29, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    656
    Joe Smith
    Jul 22, 2004
  2. Leon
    Replies:
    2
    Views:
    545
  3. keto
    Replies:
    0
    Views:
    1,001
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    374
    David Cournapeau
    Dec 30, 2008
  5. Matt Kruse
    Replies:
    5
    Views:
    322
    Richard Cornford
    Sep 9, 2003
Loading...

Share This Page