J
Justin C
$deity knows if I've got the terminology right on this. I'm trying to
tidy up an old program that extracts some stuff from one Excel
spreadsheet, munges it a little, and then puts it into another
spreadsheet. My method of tidying is, in this instance, to take all of
the bits and put them into subroutines so that the main program looks a
lot cleaner.
So I *had*:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $file = 'delete_me.xls';
my $workbook = Spreadsheet::WriteExcel->new($file);
my $excel = $workbook->add_worksheet();
for my $row ( 0 .. 99 ) {
for my $col ( 0 .. 9 ) {
my $str = $row * $col;
$excel->write_string($row, $col, $str);
}
}
That works as expected. I wanted to 'tidy away' the creation of the file and worksheet (I've also got some formatting coding there too in my real program). So here's what I made:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $file = 'delete_me.xls';
my $excel = create_excel_file($file);
for my $row ( 0 .. 99 ) {
for my $col ( 0 .. 9 ) {
my $str = $row * $col;
$excel->write_string($row, $col, $str);
}
}
sub create_excel_file {
my $of = shift;
my $workbook = Spreadsheet::WriteExcel->new($of);
my $worksheet = $workbook->add_worksheet();
return $worksheet;
}
This creates an empty file, the returned worksheet object doesn't get
anything printed to it, nor do I get any errors or warnings. I tried
returning \$worksheet but then the $exel->write_string line complained
"Can't call method ... on an unblessed reference".
This is probably where we see gaping holes in my Perl understanding. I
think this about where I got to in the Alpaca before I stopped having
free time to spend furthering my Perl education.
I did think of putting the sub contents in a BEGIN block, and lose it at
the end of the program, but I don't think that's very elegant (and I'm
not certain I could make that work either). Is there another way, or do
I have to put up with it how it is?
Justin.
tidy up an old program that extracts some stuff from one Excel
spreadsheet, munges it a little, and then puts it into another
spreadsheet. My method of tidying is, in this instance, to take all of
the bits and put them into subroutines so that the main program looks a
lot cleaner.
So I *had*:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $file = 'delete_me.xls';
my $workbook = Spreadsheet::WriteExcel->new($file);
my $excel = $workbook->add_worksheet();
for my $row ( 0 .. 99 ) {
for my $col ( 0 .. 9 ) {
my $str = $row * $col;
$excel->write_string($row, $col, $str);
}
}
That works as expected. I wanted to 'tidy away' the creation of the file and worksheet (I've also got some formatting coding there too in my real program). So here's what I made:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $file = 'delete_me.xls';
my $excel = create_excel_file($file);
for my $row ( 0 .. 99 ) {
for my $col ( 0 .. 9 ) {
my $str = $row * $col;
$excel->write_string($row, $col, $str);
}
}
sub create_excel_file {
my $of = shift;
my $workbook = Spreadsheet::WriteExcel->new($of);
my $worksheet = $workbook->add_worksheet();
return $worksheet;
}
This creates an empty file, the returned worksheet object doesn't get
anything printed to it, nor do I get any errors or warnings. I tried
returning \$worksheet but then the $exel->write_string line complained
"Can't call method ... on an unblessed reference".
This is probably where we see gaping holes in my Perl understanding. I
think this about where I got to in the Alpaca before I stopped having
free time to spend furthering my Perl education.
I did think of putting the sub contents in a BEGIN block, and lose it at
the end of the program, but I don't think that's very elegant (and I'm
not certain I could make that work either). Is there another way, or do
I have to put up with it how it is?
Justin.