Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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;

In reply to Structuring multiple CGI::Application modules II by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.