http://www.perlmonks.org?node_id=154176

axelrose has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm trying to encapsulate Spreadsheet::WiteExcel functionality in a larger script. It may be a silly mistake but in the moment I cannot succeed writing some cells...

I get an empty Excel worksheet. The module itself works. Writing in the new() constructor $worksheet->write( 1, 0, 42 ) yields the expected result. Using my writerow() method though seems useless.

Can you please point me to my misunderstanding?

Many thanks, Axel.


#!perl -w use strict; my $tmpl = ExcelWriter->new(); my @row = ( 1, 2, 3 ); $tmpl->writerow( \@row ); BEGIN { package ExcelWriter; use Spreadsheet::WriteExcel; my $tmpfile = sprintf( "/tmp/%d.%d.xls", time % 1000, int rand(1000) ); sub new { my $class = shift; my $workbook = Spreadsheet::WriteExcel->new($tmpfile) or die $!; my $worksheet = $workbook->addworksheet('Data'); bless \$worksheet, $class; } sub writerow { my ( $self, $row ) = @_; my $worksheet = $$self; for ( 0 .. @$row - 1 ) { $worksheet->write( 0, $_, $row->[$_] ); } } }
To check you need to open the resulting tmpfile. I'm testing with MacPerl5.6.1 btw.