Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Excel-Writer-XLSX CPAN distribution.

Report information
The Basics
Id: 73724
Status: resolved
Priority: 0/
Queue: Excel-Writer-XLSX

People
Owner: Nobody in particular
Requestors: bailey [...] mail.newman.upenn.edu
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.43
Fixed in: (no value)



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__ + +
On Tue Jan 03 23:03:44 2012, CBAIL wrote: Show quoted text
> The Excel::Writer::XLSX::Workbook::close() function may not return a > true value if the output file handle wasn't opened in new().
Hi Charles, Thanks for the patch, in particular the tests. Rather than return the value from flush(), which some filehandles may not have, (there is always some exceptions with filehandles), I may just have close return 1. Any objections to that. John. --
Subject: Re: [rt.cpan.org #73724] close() may incorrectly report failure for user filehandles
Date: Wed, 4 Jan 2012 08:42:03 -0500
To: bug-Excel-Writer-XLSX [...] rt.cpan.org
From: Charles Bailey <bailey [...] newman.upenn.edu>
On Jan 04, 2012, at 07:02 , John McNamara via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=73724 > > > On Tue Jan 03 23:03:44 2012, CBAIL wrote:
>> The Excel::Writer::XLSX::Workbook::close() function may not return a >> true value if the output file handle wasn't opened in new().
> > Hi Charles, > > Thanks for the patch, in particular the tests. > > Rather than return the value from flush(), which some filehandles may > not have, (there is always some exceptions with filehandles), I may just > have close return 1. > > Any objections to that.
None at all. I'd worried a bit about that, too: well-behaved filehandles should inherit the method from IO::Handle, but I'm not sure they'll all do something useful with it. Is it worth attempting a flush, then returning 1, or is that best left to the caller who passed in a filehandle to manage? -- Regards, Charles Bailey < bailey _on_ newman _dot_ upenn _dot_ edu >
On Tue Jan 03 23:03:44 2012, CBAIL wrote: Show quoted text
> The Excel::Writer::XLSX::Workbook::close() function may not return a > true value if the output file handle wasn't opened in new().
Hi Charles, I've put a fix for this into version 0.44 of Excel::Writer::XLSX. Can you test it when you get a chance and let me know if I can close this issue. Regards, John. --
Subject: Re: [rt.cpan.org #73724] close() may incorrectly report failure for user filehandles
Date: Thu, 5 Jan 2012 08:46:53 -0500
To: bug-Excel-Writer-XLSX [...] rt.cpan.org
From: Charles Bailey <bailey [...] newman.upenn.edu>
On Jan 05, 2012, at 05:47 , John McNamara via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=73724 > > > I've put a fix for this into version 0.44 of Excel::Writer::XLSX. > > Can you test it when you get a chance and let me know if I can close > this issue.
Thanks. I've confirmed that it fixes the failure I saw here. Many thanks for Excel::Writer::XLSX as well! -- Regards, Charles Bailey < bailey _on_ newman _dot_ upenn _dot_ edu >
That's great. Thanks for the report. John. --