On Apr 24, 2006, at 11:46 AM, Cees Hek wrote:

On 4/24/06, Ed Pigg <[EMAIL PROTECTED]> wrote:
Hi list,

I'm trying to get the concept of creating a super class for my
application and understand what elements need to be included in the
base application module.

Let's say I want to create a web app called MyApp. My Directory
structure looks like this ...

/MyApp
        Base.pm
        User.pm
        Inventory.pm
        Other.pm

My Base class would be

package MyApp::Base.pm
use base 'CGI::Application';

sub cgiapp_init {...};
sub cgiapp_prerun {...};
sub _some_common_code {...};

1;

Then all of my other packages will contain the run modes for that
module and can call methods from the base class as long as they are
setup as follows

package MyApp::User.pm;
use base 'MyApp::Base';

sub cgiapp_init {
        my $self = shift;
        $self->start_mode( 'display_users' );
        $self->tmpl_path( set_template_path_here );
$self->run_modes( qw/ display_users display_user_form edit_user /);
        ... module specific init items here
}

sub display_users {...}
sub display_user_form {...}
sub edit_user {...}
1;

Am I understanding the layout correctly?

Yes, except that in your subclass example, the fact that you create a
new 'cgiapp_init' method will override the 'cgiapp_init' method in
your base class.  That means the 'cgiapp_init' method in your base
class will not get executed.  That may be desired, but if it is not,
then you can call the 'cgiapp_init' method in the base class directly
by adding the following line to your 'cgiapp_init' method in your
subclass:

$self->SUPER::cgiapp_init(@_);

So how I put things like session initialization, database connection, etc.. in the SUPER::cgiapp_init sub. So my SUB CLASS would be something like

sub cgiapp_init {
        my $self = shift;
        $self->SUPER::cgiapp_init(@_);
        $self->start_mode();
        $self->run_modes( ... );
}

Ed Pigg

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/cgiapp@lists.erlbaum.net/
             http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to