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

I am completely new to Perl, as in, just started reading a book about it one week ago. Other than that, my background is in computer science although I have never applied my degree skills professionally in the past, and now I am trying to. My question is this... I am trying to write my first CGI Perlscript and am just wanting to do something easy, so I thought a phone directory would be a simple start. I would like to be able to add, delete, modify and search for numbers by name (either first or last name). I understand the idea of one CGI script calling my run modes, but how are the run modes passed??? Are they in the HTML forms that I will need to create? Also, I was wondering if someone would be willing to shell out what this program looks like and I can take it from there? It seems like I understand the pieces of the puzzle, but just not how they all fit together. I would like to use the HTML Template module and DBI as well. Anyone who can help would be my savior...so far my code looks like crap and this idea of "my $self = shift;" within run modes is mind blowing to me. Please HELP!!!! Thanks in advance,
  • Comment on Question on CGI: Application and Run Modes

Replies are listed 'Best First'.
Re: Question on CGI: Application and Run Modes
by samtregar (Abbot) on Jul 18, 2006 at 21:37 UTC
    Which book did you start a week ago? Did you finish it? I think you're probably trying to do too much too soon. A good book on basic CGI coding is O'Reilly's CGI Programming with Perl, but it won't do you much good till you've mastered basic Perl. For that, Learning Perl is a good place to start, but since you have a CS background you might find Programming Perl more your speed.

    -sam

Re: Question on CGI: Application and Run Modes
by b10m (Vicar) on Jul 18, 2006 at 21:59 UTC

    You might want to look at the CGI::Application POD (and the CGI::Application HomePage). This will explain how to use runmodes rather well. Also have a look at the example CGI::Application::Mailform.

    Runmodes can either be passed on by a variable (usually named rm), or by $ENV{'PATH_INFO'}. See the documentation on mode_param().

    It might help to specify your problem a little more in detail, for I -and I'm sure there are many other monks with me here- don't really get your "question".

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Question on CGI: Application and Run Modes
by hesco (Deacon) on Jul 19, 2006 at 09:59 UTC
    rhesa and jZed suggest that I should have spent more time working through the errors and learning this useful tool that was already costing me the good will of a client and a coding partner as I plowed through trying to make it work.

    I did work through the perldoc. I did build test scripts. I did bring the errors and issues I encountered to perlmonks, to the CGI::App list and to the relevant irc channel at the time.

    http://www.perlmonks.org/?node_id=537687
    http://www.perlmonks.org/?node_id=546793
    http://www.perlmonks.org/?node_id=550276
    http://www.perlmonks.org/?node_id=550504
    where jdtoronto warned me off CGI::App w/ CGI::FB

    That a tool works for many does not make it ideal for all. In the end I felt it was more important to deliver the requirements than to learn a complex new module.

    CGI::App on its own may be a fine tool, but my efforts to combine it with CGI::App::Plugin::Authentication and CGI::FormBuilder proved a mistake I'm happy to be past. Cees Hek and others were very helpful responding to my questions.

    But in the end, with the clock ticking and a client growing impatient, it became more important to deliver a working product than to write the most elegant code possible.

    if( $lal && $lol ) { $life++; }
      Ah, ok, sounds like you did lots of research and shared your results. And sometimes, for whatever reason, things just don't work out with a module and you need to role your own replacement. Your method of making the replacement compatible with the original is a great way to go. So I reduce my criticism of your earlier post to simply saying a pointer to the previous discussions would have been helpful.
      I think that you problem was lack of time? Sometimes our brain simply needs some time to "digest" stuff. You may understand things, but untill you start using them - you can't say you know them.

      I mean, if you were already used to CGI::Application, there wouldn't be a - would it?


      Have you tried freelancing? Check out Scriptlance - I work there. For more info about Scriptlance and freelancing in general check out my home node.
Re: Question on CGI: Application and Run Modes
by stonecolddevin (Parson) on Jul 20, 2006 at 02:49 UTC
    I recommend looking at the POD, copying and pasting the skeleton demo, and going from there. It's quite a comprehensive read, and it's pretty much all you need to

    become quite proficient with C::A.

    I've done a good deal of work with C:A, and i've found myself consistently going back to the POD when i have a question.

    meh.
Re: Question on CGI: Application and Run Modes
by hesco (Deacon) on Jul 19, 2006 at 03:06 UTC
    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++; }
      I find your comments about CGI::Application less than helpful. You mention persistent errors but don't tell us what they were. If this were an untested module we could perhaps conclude from your posting that the module might have problems. But many people have had good success with it so we are left wondering whether you stumbled on an uncommon bug or whether you just didn't fully understand the module. If you think you've discovered a bug, why not post a SOPW and let us examine it so we can all learn from your experience. Leaving it at this rumor stage doesn't really help anyone.
      I think you should seriously reconsider this approach. Ask for help on the errors you encountered (here on PM, or on the cgiapp mailing list, or even in #cgiapp on irc.perl.org). I promise you that cgiapp will make your life a lot easier than this blob of code.