object oriented perl ?

N

ngoc

Hi
This code does not work. It only creates test.xls, but write nothing in
it.I don't know why. Please help me. Thanks a lot.
test.pl
#/usr/bin/perl
use Test;
new Test('/home/my/test.xls');



Test.pm
package Test;
use strict;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Big;
use vars qw {$workbook $sheet1 $title_format};

sub new {
my $self = {};
my ($class,$file_name) = @_;
bless $self, "Test";
$workbook = Spreadsheet::WriteExcel::Big -> new ($file_name);
&sheet;
&format;
&title ('test_sid', 'test_project');
return $self;
}

sub sheet {
$sheet1 = $workbook -> add_worksheet("test");
#Set the width of the first column in Sheet1
$sheet1 -> set_column('A:A', 30);
$sheet1 -> set_column('B:M', 20);

#Set Sheet1 as the active worksheet
$sheet1 -> activate();
}

sub format {
# title format
$title_format = $workbook -> add_format();
$title_format -> set_bold ();
$title_format -> set_size (16);
$title_format -> set_color ('blue');
$title_format -> set_align ('left');

}

sub title {
my $sid_name = shift;
my $project_name = shift;
$sheet1 -> write(0, 0, $sid_name, $title_format);
$sheet1 -> write(0, 1, $project_name, $title_format);
$sheet1 -> write(1, 0, "TEST1", $title_format);
$sheet1 -> write(2, 0, "TEST2", $title_format);
}

1;
 
A

Anno Siegel

ngoc said:
Hi
This code does not work. It only creates test.xls, but write nothing in
it.I don't know why. Please help me. Thanks a lot.

For one, your code is devoid of "warnings". Switch them on, at least
when code gives you trouble.
test.pl
#/usr/bin/perl
use Test;
new Test('/home/my/test.xls');

That is the "indirect object" syntax of method calls. While it looks
nice, it can lead to ambiguities and is better avoided.

Also, normally you keep the result of a call to new(). In a productive
program, this should read

my $test_sheet = Test->new( '/home/my/test.xls');
Test.pm
package Test;
use strict;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Big;
use vars qw {$workbook $sheet1 $title_format};

You seem to use these global variables to hand objects from one
routine to another. That isn't how objects are supposed to work.
They are passed as the first parameter in a method call, and that
is how methods should learn about them.
sub new {
my $self = {};
my ($class,$file_name) = @_;
bless $self, "Test";

Bless it into $class instead of fixed "Test", or no-one will be
able to inherit from your class. In OO it is almost always an
error (or a deliberate cop-out) to bless into a fixed class.
$workbook = Spreadsheet::WriteExcel::Big -> new ($file_name);

Here you are using $workbook to communicate the current object to
sheet(). Instead, "sheet" should be a method and called as

$workbook->sheet;
&sheet;
&format;
&title ('test_sid', 'test_project');


Apart from the method/object issue, the ampersand in front of the
sub calls shouldn't be there. That form is a special case and should
only be used when its features (suppression of prototypes, use of
the current @_) are wanted.
return $self;
}

sub sheet {
$sheet1 = $workbook -> add_worksheet("test");
#Set the width of the first column in Sheet1
$sheet1 -> set_column('A:A', 30);
$sheet1 -> set_column('B:M', 20);

#Set Sheet1 as the active worksheet
$sheet1 -> activate();
}

sub format {
# title format
$title_format = $workbook -> add_format();
$title_format -> set_bold ();
$title_format -> set_size (16);
$title_format -> set_color ('blue');
$title_format -> set_align ('left');

}

sub title {
my $sid_name = shift;
my $project_name = shift;
$sheet1 -> write(0, 0, $sid_name, $title_format);
$sheet1 -> write(0, 1, $project_name, $title_format);
$sheet1 -> write(1, 0, "TEST1", $title_format);
$sheet1 -> write(2, 0, "TEST2", $title_format);
}

1;

I suppose, "sheet", "format" and "title" should be methods as well.

I don't know why your program doesn't write to the output file,
but the way you are handling parameters isn't OO, it isn't even
good functional programming. Global variables should be restricted
as much as possible. They are not a good argument passing mechanism,
far less an object passing mechanism for methods.

Anno
 
J

Jim Cochrane

You seem to use these global variables to hand objects from one
routine to another. That isn't how objects are supposed to work.
They are passed as the first parameter in a method call, and that
is how methods should learn about them.

Or, in some cases, it is appropriate to store a reference to another
object as part of the data for 'this' object (AKA attributes). This is
especially the case when these 'attributes' make sense in terms of the
data abstraction that you are designing with respect to the current class
you are working on. The fields for such attributes are usually set via
arguments to the constructor and/or via set_... methods. (And if you
are not thinking in terms of a data abstraction, AKA abstract data type,
then this is another step you should consider if you want to produce an
OO design.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top