Re: Unblessed reference.
- From: Justin C <justin.0911@xxxxxxxxxxxxxx>
- Date: Wed, 09 Dec 2009 17:10:39 -0000
On 2009-12-08, sln@xxxxxxxxxxxxxxx <sln@xxxxxxxxxxxxxxx> wrote:
On Tue, 08 Dec 2009 18:45:19 -0000, Justin C <justin.0911@xxxxxxxxxxxxxx> wrote:
^^^^^^^^^^^^^^^^^^^
my ($worksheet, $format) = create_excel_file();
Never worked with this module, just guessing.
You return reference's (\$ws, \$format).
$workbook->add_worksheet(); and
$wb->add_format(), return references, then you
are returning references to references.
When you dereference by adding the '->' arrow,
$worksheet->'something', it is looking for a method
of a class, an array, hash, ... not another reference.
What happened to $workbook? Don't need it anymore?
Its never stored. Unless of course $workbook->add_worksheet();
creates worksheet object as a wrapper and stores itself ($self)
in a variable of the worksheet object. But that seems unlikely.
I admit that this is all a grey area to me, but I'm following examples
in the module, and that's how it says to do it. I think $worksheet
contains some reference back to the $workbook (so that it knows which
workbook it is, as there could be more than one).
artist_chart() ;^^^^^^^^^
sub artist_chart {
my ($position, $last_month, $artist) = ('7', '4', 'Banksy');
populate_spreadsheet(0, $position, $last_month, $artist);
}
sub create_excel_file {
my $fname = "/var/local/chart/boris.xls";
my $workbook = Spreadsheet::WriteExcel->new($fname);
my $ws = $workbook->add_worksheet();
my $format = set_sheet_table_borders($workbook);
Goes away, lost forever!
Other than to put a worksheet in it, and add formatting to it, it's not
needed - unless I want to explicitly close it.
return (\$ws, \$format);^^^^^^
}
sub populate_spreadsheet {
my $start_col = shift;
my $items = \@_;
my $row = $_[0] + 1;
$worksheet->write_row($row,$start_col,$items,$format);
@_ appears to be singularly scoped to each function, so this
cannot be itterated. Its no big deal, but generally, create
an independent (new) array reference everytime this gets
called .. my $items = [@_];
Also, I'm sure you are aware that when items is assigned, @_
contains ($position, $last_month, $artist).
The ->write_row function (?) will populate a spreadsheet row, starting
at row $row, column $start_col and write each of @{$items} across the
columns until it runs out of @{$items}. For some reason it wants an
arrayref, not the array (according to the documentation).
}^^
sub set_sheet_table_borders {
my $wb = shift;
Here you pass in workbook object, then later it is discarded.
In the function populate_spreadsheet(),
you use the global variables ($worksheet, $format).
This does not have general portability and its hard to tell
where/what these variables are.
I would try to consistently generalize helper functions without
the use/need of globals.
If you have groups of global variables specific to each other,
as some result of making multiple objects, maybe you could store
them in an array of hashs'.
I readed perlref from time to time, each time I understand a little
more. I have a feeling that now I'm looking more at OO programming, and
using objects, I'm going to understand a bit more. I'll give it another
read, and also perlreftut.
It's all going in slowly. I love it when I go back to my old code and
replace twenty lines with just a couple. Even though I've a long way to
go, I feel that I have still come quite far.
Thanks for the help. Also, thanks to others who replied but to whom I
have not followed up, repetition helps get the information to stick!
Justin.
--
Justin C, by the sea.
.
- References:
- Unblessed reference.
- From: Justin C
- Re: Unblessed reference.
- From: sln
- Unblessed reference.
- Prev by Date: FAQ 4.60 How do I sort a hash (optionally by value instead of key)?
- Next by Date: Re: Unblessed reference.
- Previous by thread: Re: Unblessed reference.
- Next by thread: Perl to Java bytecode
- Index(es):
Relevant Pages
|