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

Using CGI::Application for the first time so I may have the wrong end of the stick here so please bear with me.

CGI::App controls which code is executed via run_modes. These are triggered by passing params to the script such as :-  http://westernesse:1234/cgi-bin/STA.pl?rm=ModifyForm&request_id=42 Whilst in that run mode - when a submit is performed from a screen generated in that runmode only the code in the subroutine relevant to the runmode should be run.

If this is right then what might cause my app to jump back to the start run mode when clicking submit.

Replies are listed 'Best First'.
Re: CGI::Application Run Modes Not Applying
by freddo411 (Chaplain) on Nov 12, 2003 at 17:25 UTC
    If you want CGI:App to perform a specific run mode when you hit submit you'll need to do a number of things:
    • Declare the run mode in your setup routine. My declaration looks like (YMMV):
      $self->run_modes([qw/ AUTOLOAD report master_edit ... etc. ... /]);
    • Put the desired run mode into a hidden variable in your form's HTML:
      ... more stuff <input type="hidden" name="rm" value="master_edit" /> ... more stuff ...
    • Finally, you'll need to code your run mode's sub. You probably know how to do that already. ;-)
    • -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

Re: CGI::Application Run Modes Not Applying
by perrin (Chancellor) on Nov 12, 2003 at 17:12 UTC
    It's not as magical as your are imagining. CGI::App just executes whatever sub you tell it to with your rm parameter. That's it. If you don't pass an rm parameter, it runs the default run mode sub.
Re: CGI::Application Run Modes Not Applying
by freddo411 (Chaplain) on Nov 12, 2003 at 17:39 UTC
    Speaking generally, you'll want to layout your run modes something like this:
    $self->run_modes([qw/ AUTOLOAD presentInvoiceEdit presentInvoice_proc Invoice_confirm /]);

    presentInvoiceEdit will put up and HTML form prefilled with data if appropriate. This HTML will have a submit button, and a hidden field directing it to rm=presentInvoice_proc.

    presentInvoice_proc will validate each submitted field, and take appropriate action (like storing it in a DB). From this run_mode, then call Invoice_confirm, which contains only presentation code.

    You could combine the last two run_modes, but I find that it is better to keep them conceptually and physically seperated. One reason for this is that the confirmation screen can then be any other useful screen, and not just a "everything OK" message screen.

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday