Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > object oriented perl ?

Reply
Thread Tools

object oriented perl ?

 
 
ngoc
Guest
Posts: n/a
 
      05-18-2004
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;
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      05-18-2004
ngoc <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
Jim Cochrane
Guest
Posts: n/a
 
      05-18-2004
In article <c8cu23$bha$(E-Mail Removed)-Berlin.DE>, Anno Siegel wrote:
> ngoc <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> 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.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.


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.)

--
Jim Cochrane; http://www.velocityreviews.com/forums/(E-Mail Removed)
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class-oriented rather than object-oriented? notnorwegian@yahoo.se Python 2 05-26-2008 04:42 PM
Free tutorial on Object Perl (Object-Oriented Perl) tim@timothyhowe.com Perl Misc 2 12-22-2005 12:30 PM
Object oriented does not mean class oriented rolo Ruby 3 04-09-2004 02:51 PM
object-oriented perl nus Perl 0 12-24-2003 07:04 AM
Hash dereferencing and object-oriented Perl Stefan Perl Misc 4 09-16-2003 08:49 PM



Advertisments