referencing a param and $self in an OO subroutine

referencing a param and $self in an OO subroutine

am 20.03.2007 16:03:36 von Marc Archuleta

I'm working on making an object oriented module in Perl for the first
time and have run into an error that I'm having trouble with:
"Can't call method "workbook" without a package or object
reference..."

In my CreateWorksheet method, I'm passing a value for the worksheet
name. But I also need to be able to reference $self at the same time.
How can I do this correctly?


sub new {
my $class = shift;
my $self = {};
$self->{SOURCEDIR} = undef;
$self->{DESTINATIONDIR} = undef;
$self->{OUTPUTFILE} = 'Invoice.xls';
$self->{WORKBOOK} = our $workbook;

bless ($self, $class);
return $self;
}

sub CreateWorksheet
{
my $self = @_;
#if (@_) { my $sheetname = shift; }
my $sheetname = shift;
print("sheetname = $sheetname");
my $WorkSheet = $self->workbook->add_worksheet($sheetname); #
ERROR OCCURS HERE
$WorkSheet->set_landscape();
$WorkSheet->fit_to_pages(1);
$WorkSheet->repeat_rows(0);
$WorkSheet->set_header("&C" . $sheetname);
return my $Worksheet;
}

Re: referencing a param and $self in an OO subroutine

am 20.03.2007 16:12:33 von paduille.4060.mumia.w+nospam

On 03/20/2007 10:03 AM, Marc wrote:
> I'm working on making an object oriented module in Perl for the first
> time and have run into an error that I'm having trouble with:
> "Can't call method "workbook" without a package or object
> reference..."
>
> In my CreateWorksheet method, I'm passing a value for the worksheet
> name. But I also need to be able to reference $self at the same time.
> How can I do this correctly?
>
>
> sub new {
> my $class = shift;
> my $self = {};
> $self->{SOURCEDIR} = undef;
> $self->{DESTINATIONDIR} = undef;
> $self->{OUTPUTFILE} = 'Invoice.xls';
> $self->{WORKBOOK} = our $workbook;
>
> bless ($self, $class);
> return $self;
> }
>
> sub CreateWorksheet
> {
> my $self = @_;
> #if (@_) { my $sheetname = shift; }
> my $sheetname = shift;

Change those lines to this:
my ($self, $sheetname) = @_;

Or you could do this:
my $self = shift;
my $sheetname = shift;

Or this:
my $self = $_[0];
my $sheetname = $_[1];


> print("sheetname = $sheetname");
> my $WorkSheet = $self->workbook->add_worksheet($sheetname); #
> ERROR OCCURS HERE
> $WorkSheet->set_landscape();
> $WorkSheet->fit_to_pages(1);
> $WorkSheet->repeat_rows(0);
> $WorkSheet->set_header("&C" . $sheetname);
> return my $Worksheet;
> }
>


HTH

Re: referencing a param and $self in an OO subroutine

am 20.03.2007 17:18:19 von Marc Archuleta

I've made some progress based on your feedback. When I tried:

my ($self, $sheetname) = @_;

it thought $self was the value of what the $sheetname was supposed to
be, throwing the following error:

Use of uninitialized value in concatenation (.) or string at line 161.
Can't locate object method "workbook" via package
"[sheetnameVariable]" (perhaps you forgot to load
"[sheetnameVariable]"?) at line 162.

the print sheetname statement had output:
"sheetname = "

So I flipped them and the print sheetname statement had output:
"sheetname = [sheetnameVariable]"

but I got this error at the same line (161):
"Can't call method "workbook" on an undefined value"

so apparently it still doesn't know what $self is. This method is
being called from another method in the module with like this:

my $Worksheet = CreateWorksheet($title);

sub CreateWorksheet
{
my ($sheetname, $self) = @_;
#if (@_) { my $sheetname = shift; }
#my $self;
#my $sheetname = shift;
print("sheetname = $sheetname");
my $WorkSheet = $self->workbook->add_worksheet($sheetname);
#line 161
#my $WorkSheet = my $workbook->add_worksheet($sheetname);
$WorkSheet->set_landscape();
$WorkSheet->fit_to_pages(1);
$WorkSheet->repeat_rows(0);
$WorkSheet->set_header("&C" . $sheetname);
return my $Worksheet;
}


On Mar 20, 11:12 am, "Mumia W." +nos...@earthlink.net> wrote:
> On 03/20/2007 10:03 AM, Marc wrote:
>
>
>
> > I'm working on making an object oriented module in Perl for the first
> > time and have run into an error that I'm having trouble with:
> > "Can't call method "workbook" without a package or object
> > reference..."
>
> > In my CreateWorksheet method, I'm passing a value for the worksheet
> > name. But I also need to be able to reference $self at the same time.
> > How can I do this correctly?
>
> > sub new {
> > my $class = shift;
> > my $self = {};
> > $self->{SOURCEDIR} = undef;
> > $self->{DESTINATIONDIR} = undef;
> > $self->{OUTPUTFILE} = 'Invoice.xls';
> > $self->{WORKBOOK} = our $workbook;
>
> > bless ($self, $class);
> > return $self;
> > }
>
> > sub CreateWorksheet
> > {
> > my $self = @_;
> > #if (@_) { my $sheetname = shift; }
> > my $sheetname = shift;
>
> Change those lines to this:
> my ($self, $sheetname) = @_;
>
> Or you could do this:
> my $self = shift;
> my $sheetname = shift;
>
> Or this:
> my $self = $_[0];
> my $sheetname = $_[1];
>
> > print("sheetname = $sheetname");
> > my $WorkSheet = $self->workbook->add_worksheet($sheetname); #
> > ERROR OCCURS HERE
> > $WorkSheet->set_landscape();
> > $WorkSheet->fit_to_pages(1);
> > $WorkSheet->repeat_rows(0);
> > $WorkSheet->set_header("&C" . $sheetname);
> > return my $Worksheet;
> > }
>
> HTH

Re: referencing a param and $self in an OO subroutine

am 20.03.2007 22:55:03 von Sherm Pendley

"Marc" writes:

> I'm working on making an object oriented module in Perl for the first
> time and have run into an error that I'm having trouble with:
> "Can't call method "workbook" without a package or object
> reference..."
>
> sub CreateWorksheet
> {
> my $self = @_;

Here's the problem. This assigns the number of elements in @_ to the $self
variable. So $self is not an object, it's 2.

> #if (@_) { my $sheetname = shift; }
> my $sheetname = shift;

This pops the first element off of @_ - which is what you wanted in $self.

Another way to write these lines:

my ($self, $sheetname) = @_;

Another:

my $self = $_[0];
my $sheetname = $_[1];

And another:

my $self = shift;
my $sheetname = shift;

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Re: referencing a param and $self in an OO subroutine

am 20.03.2007 22:59:29 von Sherm Pendley

"Marc" writes:

> so apparently it still doesn't know what $self is. This method is
> being called from another method in the module with like this:
>
> my $Worksheet = CreateWorksheet($title);

There is no $self to know about in the above - you're calling a function,
not a method. To call a method, you need to specify the object or class
whose method you're calling, like this:

my $result = $object->method();

> sub CreateWorksheet
> {
> my ($sheetname, $self) = @_;

This is backwards. $self is always the first argument to a method.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Re: referencing a param and $self in an OO subroutine

am 21.03.2007 00:38:12 von paduille.4060.mumia.w+nospam

On 03/20/2007 11:18 AM, Marc wrote:
> I've made some progress based on your feedback. When I tried:
>
> my ($self, $sheetname) = @_;
>
> it thought $self was the value of what the $sheetname was supposed to
> be, throwing the following error:
>
> Use of uninitialized value in concatenation (.) or string at line 161.
> Can't locate object method "workbook" via package
> "[sheetnameVariable]" (perhaps you forgot to load
> "[sheetnameVariable]"?) at line 162.
>
> the print sheetname statement had output:
> "sheetname = "
>
> So I flipped them and the print sheetname statement had output:
> "sheetname = [sheetnameVariable]"
>
> but I got this error at the same line (161):
> "Can't call method "workbook" on an undefined value"
>
> so apparently it still doesn't know what $self is. This method is
> being called from another method in the module with like this:
>
> my $Worksheet = CreateWorksheet($title);
>

That's not how you call methods. Try this:

my $Worksheet = $self->CreateWorksheet($title);

Read "perldoc perlboot" again.

> sub CreateWorksheet
> {
> my ($sheetname, $self) = @_;

If this is a method, $self will go first.

> [...]