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



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 CreateWork*** method, I'm passing a value for the work***
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 CreateWork***
{
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
$Work***->set_landscape();
$Work***->fit_to_pages(1);
$Work***->repeat_rows(0);
$WorkSheet->set_header("&C" . $sheetname);
return my $Work***;
}



HTH
.