http://qs321.pair.com?node_id=370263

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

First, thanks for all the replies to Structuring multiple CGI::Application modules. This rewrite is an attempt to incorporate the suggestions from that node. Note that I have studied the referenced related nodes and documentation. Unfortunately, this is also my first attempt at OOP which is probably the reason for my current problem. Either that or I've stumbled over yet another aspect of C::A that eludes me.

The problem is that although the login sub is inherited/invoked, the program seems to exit from there. By what mechanism does the app return to the original runmode invoked by the initial cgi script ('show_survey')? Do I have to store the initial value as a webapp param (if so, where? the MyApp or MyAuth module?)and then execute that runmode from the login sub? I'm pleased with the progress so far. Hopefully this is just something silly I'm overlooking. Thanks for your continued patience.

Here is the revised code:

cgi script

#!/usr/local/bin/perl use MyApp; my $webapp = MyApp->new(); $webapp->start_mode('show_survey'); $webapp->run;

authentication C::A

package MyAuth; use base 'CGI::Application'; use strict; use warnings; sub cgiapp_init { my $self = shift; # set the name of the run mode CGI param $self->mode_param('rm'); # shared run modes $self->run_modes([ 'login', 'AUTOLOAD' ]); } sub cgiapp_prerun { my $self = shift; my $runmode = shift; my $authorized = $self->param('authorized'); unless ($authorized) { $self->prerun_mode( 'login' ); } } sub login { my $self = shift; my ($error) = @_; my $output = "<p><H3>sub MyAuth::login entered</H3></p>"; my %params; $params{'ERROR'} = $error if defined($error); # load/process login template # for test purposes assume user authenticates successfully $self->param('authorized', 1); $output .= $self->dump_html(); return $output; } sub AUTOLOAD { my $self = shift; my $output = "<p><H3>sub MyAuth::AUTOLOAD entered</H3></p>"; $output .= "<p><H3>### ERROR - Invalid runmode '" . $self->mode_param('rm') . "'</H3></p>"; return $output; } 1;

survey C::A

package MyApp; use base 'MyAuth'; use strict; use warnings; sub setup { my $self = shift; my $output = "<p><H3>sub MyApp::setup entered</H3></p>"; $self->start_mode('show_survey'); $self->run_modes([ 'show_survey', 'save_survey' ]); return $output; } sub show_survey { my $self = shift; my $output = "<p><H3>sub MyApp::show_survey entered</H3></p>"; return $output; } sub save_survey { my $self = shift; my $output = "<p><H3>sub MyApp::save_survey entered</H3></p>"; return $output; } 1;