Qiang has asked for the wisdom of the Perl Monks concerning the following question:

hi, i am trying to take out the commonly used code from a probject and make it usable to other projects as well. they are basically utility modules. the way ii have done it is to have a wrapper module that provide access to all the other utility modules. here is a simplified version. then i simple access the wrapper module for all utility methods.

following are a simple verison and i would like to get some opinion from someone with more OO experience.

here is how I will call my utility methods in CGI::Application controller. i like it this way because i will have access to the utility methods by simple $self->sis->method call.

package MyApp::ControllerOne; use base 'MyApp::Base'; $self->sis->add_event("event name"); $self->sis->log_event("message"); $self->sis->uuid(); package MyApp::Base; use base 'CGI::Application'; sub sis { my $self = shift; unless ( $self->{__sis} ) { $self->{__sis} = Company::Dept->new($dbh, $uname); } return $self->{__sis}; }
here is a wrapper module to all the other utility modules:
package Company::Dept; sub new { my $class = shift; my $self = {}; my ($dbh, $uname) = @_; $self->{ __dbh } = $dbh; $self->{ __uname } $uname; bless ($self, $class); return $self; } sub log_event { my ($self, $msg) = @_ unless ( $self->{__elog} ) { $self->{__elog} = Company::Dept::EventLogging->new( $self->dbh +, $self->uuid ) ; } $self->{__elog}->log_event( $msg ); } sub add_event { my ($self, $msg) = @_ unless ( $self->{__elog} ) { $self->{__elog} = Company::Dept::EventLogging->new($self->dbh, + $self->uuid ) ; } $self->{__elog}->add_event( $msg ); } sub uuid { my $self = shift; unless ( $self->{__uuid} ) { $self->{__uuid} = Company::Dept::UUID->uuid($self->dbh, $uuid +) ; } return $self->{__uuid} ; } sub dbh { return $self->{__dbh} } sub uname {return $self->{__uname} }
here is utility module:
package Company::Dept::EventLogging; sub add_event { } sub log_event { } package Company::Dept::AnotherUtilityModule; sub aaa { } sub bbb {}
UPDATE: fixed the sub sis.

Replies are listed 'Best First'.
Re: OO code reuse in CGI::Application
by Narveson (Chaplain) on Mar 17, 2008 at 18:15 UTC
    $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}; }
      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)