From: Justin C on
On 2009-12-08, Ben Morrow <ben(a)morrow.me.uk> wrote:
>
> Quoth Justin C <justin.0911(a)purestblue.com>:
>>
>> I'm using Spreadsheet::WriteExcel, and (I think) I've created a
>> reference to the worksheet. When I try to use this elsewhere I get:
>> Can't call method ... on unblessed reference at ...
>>
>> This is the minumum I've been able to cut the code down to (sorry it's
>> not shorter). The problem line is 26. As per the Spreadsheet::WriteExcel
>> documentation I've created the object, and it's a reference to that
>> object that I've passed from a subroutine to Main:: to pass on to other
>> subroutines.
>
> An object is already a reference. You don't need to take another layer
> of reference, just return the object.

I think I'm getting the hang. I've been using references for ages,
hashrefs, arrayrefs, but whenever I use objects I get a bit confused.
It's coming together slowly now that I'd started working through the OO
documentation.


>> my ($worksheet, $format) = create_excel_file();
>>
>> artist_chart() ;
>
> Don't pass parameters to subs through globals. It's very confusing.
>
> artist_chart($worksheet, $format);

OK, sounds sensible - I really should re-read perlstyle.

Thank you for the suggestions.

Justin.

--
Justin C, by the sea.
From: Tad McClellan on
Justin C <justin.0911(a)purestblue.com> wrote:
> On 2009-12-08, Ben Morrow <ben(a)morrow.me.uk> wrote:
>> Quoth Justin C <justin.0911(a)purestblue.com>:


>>> my ($worksheet, $format) = create_excel_file();
>>>
>>> artist_chart() ;
>>
>> Don't pass parameters to subs through globals. It's very confusing.
>>
>> artist_chart($worksheet, $format);
>
> OK, sounds sensible - I really should re-read perlstyle.


Though rereading perlstyle is certainly a good idea, it does not
address the issue that Ben pointed out.

Probably because passing arguments rather than communicating with
subroutines via global variables is not an element of Perl style,
it is an element of any-programming-language style.

That is, it is fundamental Computer Science.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
From: Justin C on
On 2009-12-08, sln(a)netherlands.com <sln(a)netherlands.com> wrote:
> On Tue, 08 Dec 2009 18:45:19 -0000, Justin C <justin.0911(a)purestblue.com> 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.
From: Justin C on
In article <slrnhhvmiu.69q.tadmc(a)tadbox.sbcglobal.net>, Tad McClellan wrote:
> Justin C <justin.0911(a)purestblue.com> wrote:
>> On 2009-12-08, Ben Morrow <ben(a)morrow.me.uk> wrote:
>>> Quoth Justin C <justin.0911(a)purestblue.com>:
>
>
>>>> my ($worksheet, $format) = create_excel_file();
>>>>
>>>> artist_chart() ;
>>>
>>> Don't pass parameters to subs through globals. It's very confusing.
>>>
>>> artist_chart($worksheet, $format);
>>
>> OK, sounds sensible - I really should re-read perlstyle.
>
>
> Though rereading perlstyle is certainly a good idea, it does not
> address the issue that Ben pointed out.
>
> Probably because passing arguments rather than communicating with
> subroutines via global variables is not an element of Perl style,
> it is an element of any-programming-language style.
>
> That is, it is fundamental Computer Science.

Unfortunately that wasn't part of my education, all my programming was
learnt post academia, under my own steam. If I'm lacking some of these
fundamentals it's because the sources I leaned from didn't cover them
adequately (or I haven't got there yet). I'm working on it, alright
already!!! :)

I did take 'computer studies' at school, I remember lots of punched
cards, BASIC, 2 Commodore PETs (yes, two!) each with a cassette for
storage, and very little else. No passing of arguments was ever covered
in the curriculum, most of us had never seen a computer before much less
used one. Fundamental for us was a *lot* more fundamental - like "what
is a computer?". Until perl the only way I knew how to call a sub was
with GOSUB (Sinclair Spectrum anyone?) - I probably didn't use those
correctly either.

Justin.

--
Justin C, by the sea.
From: Tad McClellan on
Justin C <justin.0912(a)purestblue.com> wrote:

> (Sinclair Spectrum anyone?)

<me> raises hand


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"