package Bloggie; use base qw(CGI::Application); use CGI::Carp qw/fatalsToBrowser/; use CGI::Application::Plugin::TT; use DB_File; sub setup { my $self=shift; $self->start_mode('start'); $self->mode_param('rm'); $self->run_modes( 'start' => 'start', 'new post form' => 'new_post_form', 'new post success' => 'new_post_success', 'redirect' => 'redirect', ) } ## overrided so CGI.pm can be replaced by CGI::Simple sub cgiapp_get_query { require CGI::Simple; CGI::Simple->new(); } ## set up preliminary stuff like opening the database. sub cgiapp_init { my $self = shift; my $dataname = "data.dbm"; my %database; dbmopen(%database, $dataname, 0666) or die "Can't open $dataname: $!\n"; $self->{DATA}=\%database; } ## displays posts sub start { my $self = shift; my $templatename = "start.tt"; my %database = %{$self->{DATA}}; my $posts = $database{POSTS}; my $vars = { "posts" => $posts, }; return $self->tt_process($templatename, $vars); } ## displays form to create new pots sub new_post_form { my $self = shift; my $templatename = "new_post_form.tt"; return $self->tt_process($templatename); } ## displayed on success of a posts ## also writes to database the post that was submitted by new_post_form sub new_post_success { my $self = shift; my $templatename = "new_post_success.tt"; my %database = %{$self->{DATA}}; my @posts=(); my $post; #get current posts from database, if they're there #if not, make an empty array. if(defined($database{POSTS})) { @posts = @{$database{POSTS}}; } #get CGI object my $query = $self->query(); #get parameters my $title = $self->param('title'); my $content = $self->param('content'); my $post = { title => $title, content => $content, }; my $vars = { title => $title, content => $content, }; push(@posts, $post); $database{POSTS}=\@posts; return $self->tt_process($templatename, $vars); } 1;