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.

In reply to OO code reuse in CGI::Application by Qiang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.