Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Starting on code for article 3.
  • Loading branch information
davorg committed Jan 1, 2016
1 parent 70b81ce commit c7c8854
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 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;
22 changes: 22 additions & 0 deletions 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 ] ];
};
21 changes: 21 additions & 0 deletions 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;
39 changes: 39 additions & 0 deletions 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;
};

0 comments on commit c7c8854

Please sign in to comment.