Skip to content

Commit c7c8854

Browse files
committed
Starting on code for article 3.
1 parent 70b81ce commit c7c8854

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

article3-1.cgi

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/perl -T
2+
3+
use strict;
4+
use warnings;
5+
use CGI ':standard';
6+
7+
my $info = path_info;
8+
9+
print header,
10+
start_html,
11+
h1('Path Info'),
12+
p('Here are the contents of $ENV{PATH_INFO}');
13+
14+
my @info = split /\//, $info;
15+
16+
print ul(li([@info]));
17+
18+
print end_html;

article3-1.psgi

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/plackup
2+
3+
use strict;
4+
use warnings;
5+
use Plack::Request;
6+
use HTML::Tiny;
7+
8+
my $app = sub {
9+
my $info = Plack::Request->new(shift)->path_info;
10+
my @info = split /\//, $info;
11+
12+
my $h = HTML::Tiny->new;
13+
my $body = $h->html(
14+
$h->body([
15+
$h->h1('Path Info'),
16+
$h->p('Here are the contents of $ENV{PATH_INFO}'),
17+
$h->ul([ $h->li(@info) ]),
18+
]),
19+
);
20+
21+
return [ 200, [ 'Content-type' => 'text/html' ], [ $body ] ];
22+
};

article3-2.cgi

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/perl -T
2+
3+
use strict;
4+
use warnings;
5+
use CGI ':standard';
6+
7+
my $time_cookie = cookie(-name=>'time',
8+
-value=>scalar localtime,
9+
-expires=>'+1y');
10+
11+
print header(-cookie => $time_cookie),
12+
start_html(-title=>'Cookie test'),
13+
h1('Cookie test');
14+
15+
if (my $time = cookie('time')) {
16+
print p("You last visited this page at $time");
17+
} else {
18+
print p("You haven't visited this page before");
19+
}
20+
21+
print end_html;

article3-2.psgi

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/plackup
2+
3+
use strict;
4+
use warnings;
5+
use Plack::Request;
6+
use Plack::Response;
7+
use HTML::Tiny;
8+
9+
my $app = sub {
10+
my $time = Plack::Request->new(shift)->cookies->{time};
11+
my $time_text;
12+
if ($time) {
13+
$time_text = "You last visited this page at $time";
14+
} else {
15+
$time_text = "You haven't visited this page before";
16+
}
17+
18+
my $res = Plack::Response->new(200);
19+
$res->cookies->{time} = {
20+
value => scalar localtime,
21+
expires => '+1y',
22+
};
23+
$res->content_type('text/html');
24+
25+
my $h = HTML::Tiny->new;
26+
my $body = $h->html([
27+
$h->head(
28+
$h->title('Cookie Test'),
29+
),
30+
$h->body([
31+
$h->h1('Cookie Test'),
32+
$h->p($time_text),
33+
]),
34+
]);
35+
36+
$res->body($body);
37+
return $res->finalize;
38+
};
39+

0 commit comments

Comments
 (0)