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

grr. today's not my day.

i have 2 different apps that use CGI::Application, and i want to reuse some of the common methods ...

after reading A question of inheritance, it might be that the inheritance tree isn't right ... but here's the code in question:

package FlierAdminReports; use strict; use base qw/ FlierReports /; sub setup { my $self = shift; $self->start_mode( 'options' ); $self->mode_param( 'rm' ); $self->run_modes( options => \&searchOpts, summary => \&orderSummary, ); }
normal setup, no?

i have a run-mode of 'detail' in FlierReports ( the 'superclass' ) ... but it's not being found.

package FlierReports; use strict; use Data::Dumper; use base qw/ FlierFunctions /; sub setup { my $self = shift; # Setup the run mode stuff $self->start_mode( 'openOrders' ); $self->mode_param( 'rm' ); $self->run_modes( openOrders => \&openOrderSummary, detail => \&openOrderDetail, markDone => \&markFinished, ); }
the attempt is to use the "detail" run mode in the subclass ( FlierAdminReports.pm ), but it get:
No such run-mode 'detail' at /usr/lib/perl5/site_perl/5.8.0/CGI/Appli +cation.pm line 133
so i need to export the subroutine? i've tried declaring the run-mode, not declaring the run-mode ( in the subclass )... nothing does "the right thing" ....

Replies are listed 'Best First'.
Re: CGI::Application and inheritance
by Belgarion (Chaplain) on May 19, 2004 at 20:22 UTC

    The way I usually do this sort of thing is place the following in the base class:

    package BaseClass; use base 'CGI::Application'; sub cgiapp_init { my $self = shift; $self->run_modes('mode1_common' => \&mode1_common, 'mode2_common' => \&mode2_common); }

    Then in the derived class I have the standard:

    package DerivedClass; use base 'BaseClass'; sub setup { my $self = shift; $self->start_mode('start'); $self->run_modes([qw(derived_mode1 derived_mode2)]); }

    As the CGI::Application documentation for cgiapp_init states:

    If implemented, this method is called automatically right before the setup() method is called. This method provides an optional initialization hook, which improves the object-oriented character- istics of CGI::Application. The cgiapp_init() method receives, as its parameters, all the arguments which were sent to the new() method.

      that's the key. i missed that in the docs.

      i've been using cgiapp_prerun .. but not cgiapp_init

Re: CGI::Application and inheritance
by dragonchild (Archbishop) on May 19, 2004 at 20:25 UTC
    If you read the docs, you'll find that you shouldn't be using subroutine references. Instead, you should be using strings as your method names.
    package FlierReports; use strict; use Data::Dumper; use base qw/ FlierFunctions /; sub setup { my $self = shift; # Setup the run mode stuff $self->start_mode( 'openOrders' ); $self->mode_param( 'rm' ); $self->run_modes( openOrders => 'openOrderSummary', detail => 'openOrderDetail', markDone => 'markFinished', ); }

    Do that in every class and everything will "just work".

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      i guess i didn't read them carefully enough.

      i read through them to see new stuff, and yes, unfortunately for me, got 'cargo cult' in some stuff ... previous incarnations of apps i've inherited use subrefs.