Click here to get back home

referencing a param and $self in an OO subroutine

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
referencing a param and $self in an OO subroutine Marc 03-20-2007
Posted by Marc on March 20, 2007, 11:03 am
Please log in for more thread options


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-> = undef;
$self-> = undef;
        $self-> = 'Invoice.xls';
$self-> = 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;
}


Posted by Mumia W. on March 20, 2007, 11:12 am
Please log in for more thread options


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-> = undef;
> $self-> = undef;
>         $self-> = 'Invoice.xls';
> $self-> = 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

Posted by Marc on March 20, 2007, 12:18 pm
Please log in for more thread options


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." <paduille.4060.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-> = undef;
> > $self-> = undef;
> > $self-> = 'Invoice.xls';
> > $self-> = 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



Posted by Sherm Pendley on March 20, 2007, 5:59 pm
Please log in for more thread options



> 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

Posted by Mumia W. on March 20, 2007, 7:38 pm
Please log in for more thread options


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.

> [...]

Similar ThreadsPosted
CGI param persists when used with FastCGI October 6, 2005, 2:09 pm
MIME::EncWords charset param February 20, 2007, 7:03 pm
"Undefined subroutine" November 21, 2004, 8:48 pm
catch_int\catch_hup subroutine February 25, 2005, 8:51 am
Options for passing Hash to a subroutine. March 30, 2005, 7:39 pm
Perl Tk:Scheduler command called subroutine fails to talk to tk widgets January 31, 2007, 2:05 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap