File Coverage

eg/lib/My/App.pm
Criterion Covered Total %
statement 15 59 25.4
branch 0 8 0.0
condition 0 6 0.0
subroutine 5 10 50.0
pod 1 5 20.0
total 21 88 23.8


line stmt bran cond sub pod time code
1             package My::App;
2 15     15   36503 use strict;
  15         35  
  15         581  
3 15     15   80 use base 'DBIx::VersionedSubs';
  15         26  
  15         6873  
4 15     15   14232 use Template;
  15         485852  
  15         505  
5 15     15   18188 use Data::Dumper;
  15         114842  
  15         6404  
6              
7             =head1 NAME
8              
9             My::App - Sample base framework implementation
10              
11             =head1 SYNOPSIS
12              
13             =head1 ABSTRACT
14              
15             This is a sample application for you to start from. Together
16             with L, it provides a very frugal
17             yet functional environment to evolve more code within your
18             web browser.
19              
20             It provides a basic dispatch mechanism, basic templates
21             and the two functions C and C to create,
22             view, and save functions.
23              
24             The source code is most instructive to be read by you
25             to get an idea of what to do with this framework. I expect that
26             you will want to implement your own handler instead of
27             extending this sample code.
28              
29             =cut
30              
31             sub index {
32 0     0 0   my ($cgi,$res) = @_;
33 0           $res->{code} = "200";
34             return +{
35 0           title => 'Hello from ' . __PACKAGE__,
36             body => 'This is a message presented to you by ' . __PACKAGE__,
37             }
38             }
39              
40             sub view {
41 0     0 0   my ($cgi,$res) = @_;
42 0           $res->{code} = "200";
43 0           my $sub = $cgi->param('sub');
44             return +{
45 0           name => $sub,
46             'sub' => __PACKAGE__->code_source->{$sub},
47             }
48             }
49              
50             sub save {
51 0     0 0   my ($cgi,$res) = @_;
52 0           $res->{code} = "302";
53 0           my $sub = $cgi->param('sub');
54 0           my $code = $cgi->param('code');
55 0           $res->{header}->{Location} = "http://localhost/view?sub=$sub";
56 0           __PACKAGE__->redefine_sub($sub,$code);
57 0           return +{};
58             }
59              
60             sub load_template {
61 0     0 0   my ($name,$cgi) = @_;
62 0           return "templates/$name.tmpl";
63             }
64              
65             my $t = Template->new();
66             sub handler {
67 0     0 1   my ($package,$cgi) = @_;
68              
69 0           warn "--- ";
70 0           my ($name,$template);
71 0           $name = "index";
72 0 0         if ($cgi->path_info =~ m!^/(\w+)!) {
73 0           $name = $1
74             };
75 0   0       $template = $cgi->param('template') || $name;
76 0           my $res = {
77             header => {
78             'Content-Type' => 'text/html',
79             },
80             code => 500,
81             message => 'Internal Server Error',
82             template => $template,
83             };
84              
85 0           my $params = do {
86 15     15   171 no strict 'refs';
  15         40  
  15         5020  
87 0           my $c = __PACKAGE__ . ":\:$name";
88 0           warn "$c()";
89 0           eval { &{$c}($cgi,$res); };
  0            
  0            
90             };
91 0 0 0       if (my $err = $@ or ! $params) {
92 0           $params = {
93             error_message => $err,
94             name => $name,
95             template => $template,
96             query => $cgi,
97             };
98 0           $res->{code} = 500;
99 0           $res->{message} = "Internal Server Error";
100 0           $res->{header}->{'Content-Type'} = "text/html";
101 0           $template = 'error';
102             };
103            
104 0           print "HTTP/1.0 $res->{code} $res->{message}\r\n";
105 0           for (sort keys %{$res->{header}}) {
  0            
106 0           print "$_: " . $res->{header}->{$_} . "\r\n";
107             }
108 0           print "\r\n";
109            
110 0 0         if (exists $res->{template}) {
111 0 0         $t->process(load_template($template), {
112             params => $params,
113             query => $cgi,
114             namespace => $package
115             }) or die $Template::ERROR;
116             } else {
117             # raw output
118 0           print $res->{body}
119             }
120             }
121              
122             1;