CC: | andy [...] andybev.com |
Subject: | "Bad file descriptor" error on closing fh |
One of my applications started to fail with "Bad file descriptor" when calling close() on a filehandle which before was operated on by PDF::Create. The application does roughly the following:
open my $OUT, ">", $file or die $!;
# ... now it chooses which of the available backends (GD, PDF::Create, Cairo...) should be used ...
# if it is PDF::Create, then roughly call it like this
my $pdf = PDF::Create->new(fh => $OUT, ...)
# ... do work on $pdf ...
$pdf->close
# ... now we're out of the backend module and again in the main function where $OUT was created ...
close $OUT or die $!;
With the change for https://rt.cpan.org/Ticket/Display.html?id=118764 the last code line started to fail.
It seems there's a discrepancy what to expect from PDF::Create's close() method. In my case, I need a method call which does all the flush work, but which does *not* close the filehandle — as the filehandle is opened outside the module, I want also to close it outside the module. On the other hand, the name "close" might suggest that the used filehandle is also closed.
To solve this problem, I propose to split the functionality of the current close() method: a new flush() method would do all the add_* method calls, but won't close the filehandle. The close() method would now just call flush() and then close the filehandle.