package MyApp;
use base 'CGI::Application';
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::AutoRunmode;
sub setup {
my $self = shift;
$self->start_mode('home');
}
# this method is a "runmode" a "screen view" if you will
sub home : Runmode {
my $self = shift;
$self->have_basic_info or return $self->forward('basic_info');
...
}
sub basic_info : Runmode {
my $self = shift;
return q{<form>
name: <input name="name" value="">
<input type="hidden" name="rm" value="_basic_info_save">
... };
}
sub _basic_info_save : Runmode {
my $self = shift;
$self->query->param('name') or die('missing name');
$self->session->param('name' => $self->query->param('name') ); ...
}
# this method is not a runmode :
sub have_basic_info {
my $self = shift;
# LOOK: you don't worry about instancing the session object, about
+making
# sure who it's for, etc etc-- you just freaking call self->session
+()
# and your object is there- cookies set.. the works- all for you!
$self->session->param('name') ? 1 : 0;
}
And in your cgi-bin/myapp.cgi ...
#!/usr/bin/perl
use MyApp;
my $app = MyApp->new;
$app->run;
# DONE
There's a learning curve. But this is hands down the fastest way to do it, while doing it "right". CGI::Application and plugins do so much work for you that it's just hard to believe such few lines of code do so damn much.. as mentioned, you don't see there instancing of the session object, setting the cookie, retrieving it.. It's done *for* you. It's really very nice.
And the code you're using has been tested retested debugged and redebugged by a legion of extremely talented people much wiser than you or I! :-)
You can use the session object as is with defaults, or you can tweak it to all heck- timeout, expiry.. etc. Just like you would a CGI::Session object- which is what you get. The plugin manages that object.
If you want to learn it as fast as possible, look up the docs to the mentioned modules, read it all top to bottom- and give it a try.
|