| 1 | package CGI::Dispatcher::Simple; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use CGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | __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 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | 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 | 0 | ▶ | sub dispatch { | ||||||||||||||||||||||||||
|
Subroutine
dispatch: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ( $self, $methods ) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my ($method, @path, @args); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $path_info = $self->cgi->path_info || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->cgi->path_info || ''
MC/DC0%0/1
$self->cgi->path_info || ''
$self->cgi->path_info
Truth table0%0/2
A
A= $self->cgi->path_info
| |||||||||||||||||||||||||||||
| 90 | 0 | my $keys = keys %$methods; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | @path = split '/', $path_info; | |||||||||||||||||||||||||||
| 93 | 0 | shift @path; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
unshift @args, pop @path and @path
MC/DC0%0/2
unshift @args, pop @path and @path
unshift @args, pop @path
@path
Truth table0%0/3
A and B
A= unshift @args, pop @pathB= @path
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | @path = () if ($method = $methods->{ '/' . join '/', @path}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } while (unshift @args, pop @path and @path); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | shift @args if @args > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $self->args(@args); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($self->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $self->begin if $self->can('begin'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | $self->$method; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | $self->end if $self->can('end'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | 0 | croak(qq!Method "$method" does not exitst.!); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =back | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Daisuke Murase E<lt>typester@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||