Nov 25

Resolving a “print() on closed filehandle” error

Category: Linux,Perl   — Published by tengo on November 25, 2009 at 9:50 am

Today I banged my head for a few minutes against a strange error: I got a print() on closed filehandle from a simple code passage:

open(my $out,">>", "/some/file") or die "File error: $!";
 print $out "sometext\n".

Can you spot the error?

After reading through two good help threads here and here, I came to the insight that something else has to be screwed up with my code. I did not accidentially close my filehandle (common fault 1) and yes, open() was successful on opening the file (common problem 2). Then it sprang to my eye: a typo (!) at the end of the print() line, a dot instead of a semicolon! Replaced it. Fixed.