in reply to Re: Multiple forms in one CGI file?
in thread Multiple forms in one CGI file?

If you're calling the same CGI program to handle different stages of a process, then you need to give it some way to distinguish which stage each call needs to process. The easiest way to do this is to have a hidden input (I usually call it 'mode') on the form.

I often find myself writing CGI programs that look a bit like this:

++. But as a piece of advice given to a newbie, even if your code is meant to be a minimal example, there are some obvious errors that may not be just as obvious to him/her, and that make it fail to even compile:

# Work out which mode we're in my $mode = param('mode'); # If we have a mode, then call that subroutine if ($mode && %modes{mode}) { $modes->{mode}->(); } else { # Otherwise call the default handler $modes->{default}->(); }

Of course it must be $mode in both places above instead of mode, and not %modes{...} but $modes{...}. (Well, unless in Perl 6!) Oh, and %mode is a hash, not a hashref, so there's a superfluous dereferencing arrow too. Incidentally I would avoid the whole hassle of a full fledged if... else with a more concise (and not less clear)

$modes{ param('mode') || 'default' }->();

(But then I'm sure you knew and just wanted to single out the various steps for instructive purposes and the benefit of the OP in this sense...)