object oriented perl ?

Discussion in 'Perl Misc' started by ngoc, May 18, 2004.

  1. ngoc

    ngoc Guest

    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;
     
    ngoc, May 18, 2004
    #1
    1. Advertising

  2. ngoc

    Anno Siegel Guest

    ngoc <> 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
     
    Anno Siegel, May 18, 2004
    #2
    1. Advertising

  3. ngoc

    Jim Cochrane Guest

    In article <c8cu23$bha$-Berlin.DE>, Anno Siegel wrote:
    > ngoc <> 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;
    [When responding by email, include the term non-spam in the subject line to
    get through my spam filter.]
     
    Jim Cochrane, May 18, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nus

    object-oriented perl

    nus, Dec 24, 2003, in forum: Perl
    Replies:
    0
    Views:
    528
  2. Replies:
    2
    Views:
    468
    Bruno Desthuilliers
    May 26, 2008
  3. rolo
    Replies:
    3
    Views:
    197
    Robert Klemme
    Apr 9, 2004
  4. Stefan
    Replies:
    4
    Views:
    158
    Mina Naguib
    Sep 16, 2003
  5. Replies:
    2
    Views:
    196
    Brian McCauley
    Dec 22, 2005
Loading...

Share This Page