diff --git a/article3-1.cgi b/article3-1.cgi new file mode 100644 index 0000000..30fdb27 --- /dev/null +++ b/article3-1.cgi @@ -0,0 +1,18 @@ +#!/usr/bin/perl -T + +use strict; +use warnings; +use CGI ':standard'; + +my $info = path_info; + +print header, +start_html, +h1('Path Info'), +p('Here are the contents of $ENV{PATH_INFO}'); + +my @info = split /\//, $info; + +print ul(li([@info])); + +print end_html; diff --git a/article3-1.psgi b/article3-1.psgi new file mode 100644 index 0000000..7851e4a --- /dev/null +++ b/article3-1.psgi @@ -0,0 +1,22 @@ +#!/usr/bin/plackup + +use strict; +use warnings; +use Plack::Request; +use HTML::Tiny; + +my $app = sub { + my $info = Plack::Request->new(shift)->path_info; + my @info = split /\//, $info; + + my $h = HTML::Tiny->new; + my $body = $h->html( + $h->body([ + $h->h1('Path Info'), + $h->p('Here are the contents of $ENV{PATH_INFO}'), + $h->ul([ $h->li(@info) ]), + ]), + ); + + return [ 200, [ 'Content-type' => 'text/html' ], [ $body ] ]; +}; diff --git a/article3-2.cgi b/article3-2.cgi new file mode 100644 index 0000000..068a907 --- /dev/null +++ b/article3-2.cgi @@ -0,0 +1,21 @@ +#!/usr/bin/perl -T + +use strict; +use warnings; +use CGI ':standard'; + +my $time_cookie = cookie(-name=>'time', + -value=>scalar localtime, + -expires=>'+1y'); + +print header(-cookie => $time_cookie), +start_html(-title=>'Cookie test'), +h1('Cookie test'); + +if (my $time = cookie('time')) { + print p("You last visited this page at $time"); +} else { + print p("You haven't visited this page before"); +} + +print end_html; diff --git a/article3-2.psgi b/article3-2.psgi new file mode 100644 index 0000000..b97cfd5 --- /dev/null +++ b/article3-2.psgi @@ -0,0 +1,39 @@ +#!/usr/bin/plackup + +use strict; +use warnings; +use Plack::Request; +use Plack::Response; +use HTML::Tiny; + +my $app = sub { + my $time = Plack::Request->new(shift)->cookies->{time}; + my $time_text; + if ($time) { + $time_text = "You last visited this page at $time"; + } else { + $time_text = "You haven't visited this page before"; + } + + my $res = Plack::Response->new(200); + $res->cookies->{time} = { + value => scalar localtime, + expires => '+1y', + }; + $res->content_type('text/html'); + + my $h = HTML::Tiny->new; + my $body = $h->html([ + $h->head( + $h->title('Cookie Test'), + ), + $h->body([ + $h->h1('Cookie Test'), + $h->p($time_text), + ]), + ]); + + $res->body($body); + return $res->finalize; +}; +