While you're at it you might reconsider giant if/else trees, even though a lot of people, even smart poeple, like/use them
I find this to read, easier to maintain
#!/usr/bin/perl --
use strict; use warnings;
use CGI::FormBuilder;
Main( @ARGV );
exit( 0 );
sub Main {
my $cgi = CGI->new;
# Our "mode" parameter determines what we do
my $mode = $cgi->param('mode') || 'default';
# Change our form based on our mode
if ($mode eq 'view') {
modeView( $cgi );
} elsif ($mode eq 'edit') {
modeEdit( $cgi );
} else {
modeDefault( $cgi );
}
}
sub modeDefault { ... }
sub modeEdit { ... }
sub modeView {
my ( $cgi ) = @_;
my $form = CGI::FormBuilder->new(
method => 'post',
fields => [qw(...)],
params => $cgi # get CGI params
);
...
if ($form->submitted && $form->validate) {
print $form->confirm;
} else {
print $form->render;
}
}
But if you go that approach, you might as well use a little standard sugar #!/usr/bin/perl --
use strict; use warnings;
use CGI::Application;
use CGI::FormBuilder;
Main( @ARGV );
exit( 0 );
sub Main {
MyAppname->new->run;
}
BEGIN {
package MyAppname;
use parent 'CGI::Application';
use CGI::Application::Plugin::AutoRunmode;
use CGI::Application::Plugin::DebugScreen;
sub What : Runmode {
CGI::FormBuilder->new->render;
}
sub Edit : Runmode {
...
}
sub View: StartRunmode {
my ( $cgiapp ) = @_;
my $form = CGI::FormBuilder->new(
method => 'post',
fields => [qw( one two three )],
params => $cgiapp->query, # get CGI params
);
if ($form->submitted && $form->validate) {
# you don't print in CGI::Application
return $form->confirm;
} else {
return $form->render;
}
}
}
|