in reply to OO code reuse in CGI::Application

$self->sis->add_event("event name"); $self->sis->log_event("message"); $self->sis->uuid(); sub sis { return Company::Dept->new($dbh, $uname); }

Every time you call a method on $self->sis you're going to construct a new instance of Company::Dept, which means you might as well not bother constructing instances at all, since you'll never retrieve any instance data.

I don't fully understand what $self is in the top three statements, but here's a way to have $self->sis persist from one statement to the next:

sub sis { my ($self) = @_; # check whether the internal representation is already defined if ( !$self->{_sis} ) { # call the constructor and save the result my ($dbh, $uname) = get_constructor_args_from_somewhere(); $self->{_sis} = Company::Dept->new($dbh, $name); } return $self->{_sis}; }

Replies are listed 'Best First'.
Re^2: OO code reuse in CGI::Application
by Qiang (Friar) on Mar 17, 2008 at 18:36 UTC
    You are right.

    i just wrote the sub for demostration.. but it is overly simplified. i have fixed my original post.

    $self is a CGI::Application object that can be accessed from my controllers(controllers are subclass of C::A)