line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Dispatcher::Simple; |
2
|
1
|
|
|
1
|
|
20801
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
3
|
1
|
|
|
1
|
|
5
|
use base qw/Class::Accessor::Fast/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
872
|
|
4
|
1
|
|
|
1
|
|
4747
|
use CGI; |
|
1
|
|
|
|
|
19694
|
|
|
1
|
|
|
|
|
8
|
|
5
|
1
|
|
|
1
|
|
57
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
415
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/args cgi/); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
CGI::Dispatcher::Simple - Simple CGI Dispacher by PATH_INFO |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# In your App |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
package MyApp; |
20
|
|
|
|
|
|
|
use base qw/CGI::Dispacher::Simple/; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub run { |
23
|
|
|
|
|
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$self->dispatch({ |
26
|
|
|
|
|
|
|
'/' => 'default', |
27
|
|
|
|
|
|
|
'/list' => 'list', |
28
|
|
|
|
|
|
|
'/add' => 'add', |
29
|
|
|
|
|
|
|
}); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub default { |
33
|
|
|
|
|
|
|
: |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
: |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# And in your CGI script |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $app = MyApp->new; |
41
|
|
|
|
|
|
|
$app->run; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 DESCRIPTION |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
This module provide you to simple dispatcher by using PATH_INFO. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
You can set some methods as hashref, PATH_INFO are keys, METHODS are values. |
49
|
|
|
|
|
|
|
like: |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
'/' => 'default', |
52
|
|
|
|
|
|
|
'/list/add' => 'add', |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
And, rest of PATH_INFO is saved in $self->args as arrayref. |
55
|
|
|
|
|
|
|
When PATH_INFO is '/list/add/foo/bar' in above example, $self->args is: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
[ 'foo', 'bar' ] |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
If you define $self->begin or $self->end methods, these are called automatically |
61
|
|
|
|
|
|
|
before/after PATH_INFO method. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
And when PATH_INFO is not defined, dispatch to '/' method. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 METHODS |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=over 4 |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item new |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub new { |
74
|
0
|
|
|
0
|
1
|
|
my $self = bless {}, shift; |
75
|
0
|
|
|
|
|
|
$self->cgi(CGI->new); |
76
|
0
|
|
|
|
|
|
$self->cgi->charset('utf-8'); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$self; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item dispatch |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub dispatch { |
86
|
0
|
|
|
0
|
1
|
|
my ( $self, $methods ) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my ($method, @path, @args); |
89
|
0
|
|
0
|
|
|
|
my $path_info = $self->cgi->path_info || ''; |
90
|
0
|
|
|
|
|
|
my $keys = keys %$methods; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
@path = split '/', $path_info; |
93
|
0
|
|
|
|
|
|
shift @path; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
0
|
|
|
|
do { |
96
|
0
|
0
|
|
|
|
|
@path = () if ($method = $methods->{ '/' . join '/', @path}); |
97
|
|
|
|
|
|
|
} while (unshift @args, pop @path and @path); |
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
shift @args if @args > 1; |
100
|
0
|
|
|
|
|
|
$self->args(@args); |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if ($self->can($method)) { |
103
|
0
|
0
|
|
|
|
|
$self->begin if $self->can('begin'); |
104
|
0
|
|
|
|
|
|
$self->$method; |
105
|
0
|
0
|
|
|
|
|
$self->end if $self->can('end'); |
106
|
|
|
|
|
|
|
} else { |
107
|
0
|
|
|
|
|
|
croak(qq!Method "$method" does not exitst.!); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=back |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 AUTHOR |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Daisuke Murase Etypester@cpan.orgE |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 COPYRIGHT |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
This program is free software; you can redistribute |
120
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
The full text of the license can be found in the |
123
|
|
|
|
|
|
|
LICENSE file included with this module. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |