I'd have to second Sam Tregar's comments. Learn to walk before you run. It was years of coding before I began to wrap my head around objects, what they are and what I can do with them and only in the last year or so that I've tried to write my own.

I just had a lousy experience with CGI::Application where in order to get our project back on track I wound up ripping out CGI::Application and coding my own ->new(), ->run() and ->cgiapp_init() methods so the rest of the code I had built around CGI::App would work. I lost a day writing those three methods. But I had already lost two trying to work through the errors with CGI::App.

The CGI::App crowd would probably moan were they to see this code, but it works, was delivered in reasonably good time and bypassed some persistent errors I encountered with the CGI::App framework.

My ->run() method uses old fashioned conditionals to determine dispatch. This was written is such a way that if I was ever able to work through the errors with CGI::App, I could swap it in in place of the three methods I wrote.

Perhaps this would be helpful.

-- Hugh

sub run { my $self = shift; my $rm_arg = shift; # || 'RegForm'; # my $reg_id = shift; my ($cfg,$dbh) = $self->cgiapp_init(); my $session = new CGI::Session() or die CGI::Session->errstr; my $reg_id = $session->param("reg_id"); my ($output,$method); my $q = new CGI; my $rm = $q->param('rm') || $rm_arg; ###l4p my $logger = Log::Log4perl->get_logger('RegForm.run'); ###l4p $logger->debug(Dumper(\$self,\$q)); ###l4p $logger->info("\$reg_id is $reg_id and \$rm is $rm."); ((defined ($q->param('_submitted_RegistrantForm'))) || ($rm eq '')) +|| die " DISPAIR \$q->param('_submitted_Registrant Form') is undef \n"; if(defined($q->param('_submitted_RegistrantForm')) && $q->param('_su +bmit') eq "Proceed_to_Meals_and_Housing"){ $rm = "RegistrantNeeds"; } elsif( (defined($q->param('_submitted_RegistrantNeeds'))) && $q->p +aram('_submit') eq "Proceed_to_Checkout"){ $rm = "RegistrantNeeds"; } elsif( (defined($q->param('_submitted_Checkout'))) && $q->param('_ +submit') eq "Recalculate_Invoice"){ $rm = "Checkout"; } elsif( (defined($q->param('_submitted_Checkout'))) && $q->param('_ +submit') eq "Proceed_to_Payment"){ $rm = "Checkout"; ###l4p $logger->info("\$reg_id is $reg_id and \$rm is $rm and we'r +e dispatching to ->payment() run mode."); } my %run_modes = ( 'RegForm' => 'registrant_form', 'RegistrantNeeds' => 'registrant_needs', 'Checkout' => 'checkout', 'Payment' => 'payment', 'Thankyou' => 'thank_you', 'Admin' => 'admin_screen', ); print STDERR "->run() method defined %run_modes.\n"; { no strict 'refs'; $method = $run_modes{$rm}; } (defined($reg_id )) && print STDERR " near the end of run(), \$reg_ +id is $reg_id\n"; (defined($reg_id )) || print STDERR " near the end of run(), \$reg_ +id is undefined\n"; if ($method eq '') { print STDERR " oh NO! undefining \$reg_id !\n"; $method = 'registrant_form'; undef($reg_id); } (defined($method )) || die " OUCH \$method is undefined in ->run()\n +"; (defined($reg_id) || $method eq 'registrant_form') || die("\$reg_id is undefined, but a registrantID is required for runmode: ->$method(). This usually means that an additional tab or window has been opened to this application. Please close all but your newest invocation of this page and try again.\n"); # print STDERR "->run() method about to pass control to $rm.\n"; ###lp4 $logger->info("->run() method about to pass control to $rm.") +; ($output,$reg_id) = $self->$method($cfg,$reg_id); ###l4p $logger->info("->run() method passed control to $rm which has + returned its output for \$reg_id # $reg_id."); # print STDERR "->run() method passed control to $rm which has retur +ned its output for \$reg_id # $reg_id.\n"; $session->param('reg_id',$reg_id); print $session->header(); print $output; print STDERR "->run() method ready to return.\n"; ###l4p $logger->info("->run() method ready to return processing for +$reg_id."); return; }
if( $lal && $lol ) { $life++; }

In reply to Re: Question on CGI: Application and Run Modes by hesco
in thread Question on CGI: Application and Run Modes by derekash913

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.