# HelloWorldCgiApp2.pm package HelloWorldCgiApp2; use lib qw(/home/redantig/perl); use base 'CGI::Application'; use HTML::Template; use strict; sub cgiapp_init { # Optional application init (such as DB connect) } sub setup { my $self = shift; $self->start_mode('mode1'); $self->run_modes( 'mode1' => 'helloworld_cgi_app1', 'mode2' => 'helloworld_cgi_app2', 'mode3' => 'helloworld_cgi_app3', ); } sub teardown { # Optional application shutdown (such as DB disconnect) } # Common stuff for all run modes sub get_common_stuff { my $this_mode = shift; my $calling_mode = shift || ''; my @other_modes; #Ugly but straightforward... @other_modes = (2, 3) if ($this_mode == 1); @other_modes = (1, 3) if ($this_mode == 2); @other_modes = (1, 2) if ($this_mode == 3); #Messages and links my $text_this = "This is run mode $this_mode"; my $text_called_from = $calling_mode ? "This mode was called from run mode $calling_mode" : ''; my $href_goto_a = 'helloworld.cgi-app2.pl?rm=mode'.$other_modes[0].'&calling_mode='.$this_mode; my $text_goto_a = "Go to run mode $other_modes[0]"; my $href_goto_b = 'helloworld.cgi-app2.pl?rm=mode'.$other_modes[1].'&calling_mode='.$this_mode; my $text_goto_b = "Go to run mode $other_modes[1]"; return ($text_this, $text_called_from, $href_goto_a, $text_goto_a, $href_goto_b, $text_goto_b); } # simple style "Hello, World!" sub helloworld_cgi_app1 { # This is run mode 1 my $this_mode = 1; # Get CGI query object my $self = shift; my $q = $self->query(); my $calling_mode = $q->param("calling_mode"); my $that_famous_string = 'Hello, world!'; # Get common stuff for all run modes my ($text_this, $text_called_from, $href_goto_a, $text_goto_a, $href_goto_b, $text_goto_b) = &get_common_stuff($this_mode, $calling_mode); #Generate HTML my $html_output = ''; $html_output .= ''."\n"; $html_output .= "\n"; $html_output .= "\n"; $html_output .= "$that_famous_string\n"; $html_output .= "\n"; $html_output .= "\n"; $html_output .= "Simple style\n"; $html_output .= "
\n"; $html_output .= "$that_famous_string\n"; $html_output .= "
\n"; $html_output .= "

$text_this

\n"; $html_output .= "

$text_called_from

\n"; $html_output .= "$text_goto_a\n"; $html_output .= "
\n"; $html_output .= "$text_goto_b\n"; $html_output .= "\n"; $html_output .= "\n"; return $html_output; } # CGI.pm-style "Hello, World!" sub helloworld_cgi_app2 { # This is run mode 2 my $this_mode = 2; # Get CGI query object my $self = shift; my $q = $self->query(); my $calling_mode = $q->param("calling_mode"); my $that_famous_string = 'Hello, world!'; # Get common stuff for all run modes my ($text_this, $text_called_from, $href_goto_a, $text_goto_a, $href_goto_b, $text_goto_b) = &get_common_stuff($this_mode, $calling_mode); my $html_output = ''; $html_output .= $q->start_html(-title => $that_famous_string); # start the HTML $html_output .= $q->i("CGI.pm-style")."\n"; # italic $html_output .= $q->br."\n"; # newline $html_output .= $q->b($that_famous_string)."\n"; # bold $html_output .= $q->br."\n"; # newline $html_output .= $q->p($text_this)."\n"; # paragraph $html_output .= $q->p($text_called_from)."\n"; # paragraph $html_output .= $q->a({href=>$href_goto_a},$text_goto_a)."\n"; # href $html_output .= $q->br."\n"; # newline $html_output .= $q->a({href=>$href_goto_b},$text_goto_b)."\n"; # href $html_output .= $q->end_html()."\n"; # end the HTML return $html_output; } # HTML::Template-style "Hello, World!" sub helloworld_cgi_app3 { # This is run mode 3 my $this_mode = 3; # Get CGI query object my $self = shift; my $q = $self->query(); my $calling_mode = $q->param("calling_mode"); my $that_famous_string = 'Hello, world!'; # Get common stuff for all run modes my ($text_this, $text_called_from, $href_goto_a, $text_goto_a, $href_goto_b, $text_goto_b) = &get_common_stuff($this_mode, $calling_mode); my $html_output = ''; # Open the html template my $template = HTML::Template->new(filename => 'helloworld.cgi-app.tmpl.html'); # Fill in some parameters $template->param( THAT_FAMOUS_STRING => $that_famous_string, TEXT_THIS => $text_this, TEXT_CALLED_FROM => $text_called_from, HREF_GOTO_A => $href_goto_a, TEXT_GOTO_A => $text_goto_a, HREF_GOTO_B => $href_goto_b, TEXT_GOTO_B => $text_goto_b, ); # parse the template $html_output .= $template->output; return $html_output; } 1;