File closure problems

  • Thread starter Jonas Andersson
  • Start date
J

Jonas Andersson

Hi,

I sometimes get weird problems with file closures in Perl:

print FILE "...a lot of data...";
$line_number=240;
close(FILE) or &error_catcher;
....
exit;

sub error_catcher {
print "Error around line $line_number. Exiting. \n";
exit;
}

Any idea why this happens? I thought Perl waited for the writing to
the file to finish before it tried to close it? What could I do in
this situation?

Thanks for your time,

JA
 
T

Tassilo v. Parseval

Also sprach Jonas Andersson:
Hi,

I sometimes get weird problems with file closures in Perl:

Note that using 'closure' in this context is a bit misleading. It's
usually used to refer to a different concept (that of functions or
function-references having access to outside variables).
print FILE "...a lot of data...";
$line_number=240;
close(FILE) or &error_catcher;
...
exit;

sub error_catcher {
print "Error around line $line_number. Exiting. \n";
exit;
}

Any idea why this happens? I thought Perl waited for the writing to
the file to finish before it tried to close it? What could I do in
this situation?

Perl just writes to a filehandle. The operating system does the work
behind the curtains. There is usually little need to wait (not in the
case of regular files certainly).

Have you considered checking why it failed? Your operating system knows
much better than people in this group:

sub error_catcher {
print "Error...: $!";
exit 1;
}

The value of $! plus the first paragraph of 'perldoc -f close' should
give you a pretty accurate idea what went wrong.

A minor nit: When your program exits due to an error, you should
indicate that by providing an exit code other than zero. It's a question
of good style and following the conventions nicely.

Tassilo
 
C

Chris Mattern

Jonas said:
Hi,

I sometimes get weird problems with file closures in Perl:

"Weird problems"? I left my ESP helmet at home today; I have
no idea what "weird problems" might refer to.
print FILE "...a lot of data...";
$line_number=240;
close(FILE) or &error_catcher;

Don't use & on subroutine calls unless you know what it does
and you want that behavior. The builtin function die does
what you're doing here except it does it much better; see
below.
...
exit;

sub error_catcher {
print "Error around line $line_number. Exiting. \n";
exit;
}

Any idea why this happens?

Why what happens? "Weird problems" is not an error description.

Incidentally, your code is poor. Better:

close (FILE) or die "Error closing file:$!";

By not finishing the string you give die with a newline, it
will automatically report the line you died on. $! will
report what error close() actually gave.
I thought Perl waited for the writing to
the file to finish before it tried to close it? What could I do in
this situation?

Thanks for your time,

JA

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top