Subject: | close() may incorrectly report failure for user filehandles |
The Excel::Writer::XLSX::Workbook::close() function may not return a
true value if the output file handle wasn't opened in new(). While
strictly accurate - close() doesn't actually close a file handle that
new didn't open, so there's no return value from CORE::close(), this
behavior makes it difficult to use close() as a finalizer to clean up
and check for errors when done constructing the Excel output.
The attached patch alters the behavior of close() slightly, so that it
will return a true value absent an error writing the workbook.
Subject: | EWXW_close.patch |
diff -BburN Excel-Writer-XLSX-0.43/lib/Excel/Writer/XLSX/Workbook.pm Excel-Writer-XLSX-0.43-patched/lib/Excel/Writer/XLSX/Workbook.pm
--- Excel-Writer-XLSX-0.43/lib/Excel/Writer/XLSX/Workbook.pm 2011-12-18 18:12:34.000000000 -0500
+++ Excel-Writer-XLSX-0.43-patched/lib/Excel/Writer/XLSX/Workbook.pm 2012-01-03 21:40:46.000000000 -0500
@@ -198,7 +198,12 @@
$self->{_fileclosed} = 1;
$self->_store_workbook();
- return CORE::close( $self->{_filehandle} ) if $self->{_internal_fh};
+ if ($self->{_internal_fh}) {
+ return $self->{_filehandle}->flush();
+ }
+ else {
+ return $self->{_filehandle}->close();
+ }
}
diff -BburN Excel-Writer-XLSX-0.43/t/workbook/sub_close.t Excel-Writer-XLSX-0.43-patched/t/workbook/sub_close.t
--- Excel-Writer-XLSX-0.43/t/workbook/sub_close.t 1969-12-31 19:00:00.000000000 -0500
+++ Excel-Writer-XLSX-0.43-patched/t/workbook/sub_close.t 2012-01-03 22:07:46.000000000 -0500
@@ -0,0 +1,42 @@
+###############################################################################
+#
+# Tests for Excel::Writer::XLSX::Workbook methods.
+#
+# reverse('(c)'), January 2012, John McNamara, jmcnamara@cpan.org
+#
+
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+
+###############################################################################
+#
+# Tests setup.
+#
+my $filename = 'autofilter00.xlsx';
+my $dir = 't/regression/';
+my $ext_filename = $dir . $filename;
+my $scalar_target;
+
+
+###############################################################################
+#
+# Test the close() method.
+#
+
+use Excel::Writer::XLSX;
+
+my $workbook = Excel::Writer::XLSX->new( $ext_filename );
+$workbook->add_worksheet();
+ok($workbook->close(), "\tWorkbook: close(), _internal_fh");
+unlink $ext_filename;
+
+open(my $fh, '>', \$scalar_target);
+$workbook = Excel::Writer::XLSX->new( $fh );
+$workbook->add_worksheet();
+ok($workbook->close(), "\tWorkbook: close(), not _internal_fh");
+
+__END__
+
+