| 1 | package CGI::Application::Plus ; | ||||||||||||||||||||||||||||
| 2 | $VERSION = 1.21 ; | ||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # This file uses the "Perlish" coding style | ||||||||||||||||||||||||||||
| 6 | # please read http://perl.4pro.net/perlish_coding_style.html | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | ; use Carp | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ######### NEW ############ | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | ; use Class::constr | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | ( { init => [ qw| cgiapp_init setup | ] | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | , no_strict => 1 | ||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | ) | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ######### GROUPS ############ | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ; use Object::groups | |||||||||||||||||||||||||||
| 21 | 8 | ▶ | ( { name => [ qw | param header_props | ] | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.8
| |||||||||||||||||||||||||||||
| 22 | , no_strict => 1 | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | , { name => 'run_modes' | ||||||||||||||||||||||||||||
| 25 | , no_strict => 1 | ||||||||||||||||||||||||||||
| 26 | , pre_process=> sub | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | { if ( ref $_[1] eq 'ARRAY' ) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | { $_[1] = { map { $_=>$_ } @{$_[1]} } | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | , { name => 'qparam' | ||||||||||||||||||||||||||||
| 33 | , default => sub | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | { eval{ scalar $_[0]->query->Vars } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | || croak qq(The query object cannot "Vars", ) | ||||||||||||||||||||||||||||
| 36 | . qq(you cannot use the "qparam" ) | ||||||||||||||||||||||||||||
| 37 | . qq(property.) | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | ) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ######### PROPERTIES ############ | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 8 | ; use Object::props | |||||||||||||||||||||||||||
| 46 | 8 | ▶ | ( { name => '__STEP' | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.7
| |||||||||||||||||||||||||||||
| 47 | , default => 0 | ||||||||||||||||||||||||||||
| 48 | , allowed => qr/::run$/ | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | , { name => 'mode_param' | ||||||||||||||||||||||||||||
| 51 | , default => 'rm' | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | , { name => 'query' | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | , default => sub { shift()->cgiapp_get_query(@_) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | , no_strict => 1 # doesn't croak if fetched too late | ||||||||||||||||||||||||||||
| 56 | , validation => sub | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | { croak qq(Too late to set the query) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | if $_[0]->__STEP > 0 # just before run | ||||||||||||||||||||||||||||
| 59 | 0 | ; 1 | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | , { name => 'runmode' | ||||||||||||||||||||||||||||
| 63 | , default => 'start' | ||||||||||||||||||||||||||||
| 64 | , validation => sub | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | { croak qq(Too late to set the run mode) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
Condition0%0/3
$_[0]->__STEP >= 2 and not +(caller 2)[3] =~ /::_run_runmode$/
MC/DC0%0/2
$_[0]->__STEP >= 2 and not +(caller 2)[3] =~ /::_run_runmode$/
$_[0]->__STEP >= 2
not +(caller 2)[3] =~ /::_run_runmode$/
Truth table0%0/3
A and not(B)
A= $_[0]->__STEP >= 2B= not +(caller 2)[3] =~ /::_run_runmode$/
| |||||||||||||||||||||||||||||
| 66 | if ( $_[0]->__STEP >= 2 # after prerun | ||||||||||||||||||||||||||||
| 67 | && (caller(2))[3] !~ /::_run_runmode$/ | ||||||||||||||||||||||||||||
| 68 | ) | ||||||||||||||||||||||||||||
| 69 | 0 | ; 1 | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | , { name => 'tmpl_path' | ||||||||||||||||||||||||||||
| 73 | , default => './tm' | ||||||||||||||||||||||||||||
| 74 | , no_strict => 1 # doesn't croak if ./tm is not a valid path | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | , validation => sub { -d or croak qq(Not a valid path) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | , { name => 'RM_prefix' | ||||||||||||||||||||||||||||
| 78 | , default => 'RM_' | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | , { name => 'header_type' | ||||||||||||||||||||||||||||
| 81 | , default => 'header' | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | , validation => sub{ /^(header|redirect|none)$/ } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 8 | , { name => 'page' | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | ) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | ######### PARAM AUTOLOAD ############ | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 8 | ; our $AUTOLOAD | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | ; sub AUTOLOAD :lvalue # Param AUTOLOADING | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 94 | 0 | { (my $n = $AUTOLOAD) =~ s/.*:// | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | ; return if $n eq 'DESTROY' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | ; @_ == 2 | ||||||||||||||||||||||||||||
| 97 | ? ( $_[0]{param}{$n} = $_[1] ) | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | : $_[0]{param}{$n} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | ######### OVERRIDE METHODS ############ | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | ; BEGIN | ||||||||||||||||||||||||||||
| 104 | 8 | ▶ | { no strict 'refs' | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 8 | ; foreach my $n qw| cgiapp_init | |||||||||||||||||||||||||||
| 106 | cgiapp_prerun | ||||||||||||||||||||||||||||
| 107 | cgiapp_postrun | ||||||||||||||||||||||||||||
| 108 | teardown | ||||||||||||||||||||||||||||
| 109 | | | ||||||||||||||||||||||||||||
| 110 | { *$n = sub {} | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | ; sub cgiapp_get_query | ||||||||||||||||||||||||||||
| 116 | { require CGI | ||||||||||||||||||||||||||||
| 117 | ; CGI->new() | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | ; sub setup | ||||||||||||||||||||||||||||
| 121 | { $_[0]->run_modes( start => \&dump_html ) | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | ######### METHODS ############ | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | ; sub run | ||||||||||||||||||||||||||||
| 128 | { my ($s, $RM) = @_ | ||||||||||||||||||||||||||||
| 129 | ; $s->__STEP = 1 | ||||||||||||||||||||||||||||
| 130 | ; unless ( defined $RM && length $RM ) # no RM from script | ||||||||||||||||||||||||||||
| 131 | { $RM = ref $s->mode_param eq 'CODE' | ||||||||||||||||||||||||||||
| 132 | ? $s->mode_param->($s) # RM from code ref | ||||||||||||||||||||||||||||
| 133 | : $s->query->param($s->mode_param) # RM from query | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | ; unless ( defined $RM && length $RM ) # no RM yet | ||||||||||||||||||||||||||||
| 136 | { $RM = $s->runmode # RM from default | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | else | ||||||||||||||||||||||||||||
| 139 | { $s->runmode = $RM # shitch RM | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | ; $s->__STEP = 2 | ||||||||||||||||||||||||||||
| 142 | ; $s->cgiapp_prerun( $RM ) # passed just for full compatibility | ||||||||||||||||||||||||||||
| 143 | ; $s->__STEP = 3 | ||||||||||||||||||||||||||||
| 144 | ; $s->_run_runmode( $RM ) | ||||||||||||||||||||||||||||
| 145 | if $RM eq $s->runmode # if unchanged by prerun | ||||||||||||||||||||||||||||
| 146 | ; $s->__STEP = 4 | ||||||||||||||||||||||||||||
| 147 | ; $s->page = \ ( my $p = $s->page ) | ||||||||||||||||||||||||||||
| 148 | unless ref $s->page | ||||||||||||||||||||||||||||
| 149 | ; $s->cgiapp_postrun( $s->page ) # passed just for compatibility | ||||||||||||||||||||||||||||
| 150 | ; my $output = $s->_send() | ||||||||||||||||||||||||||||
| 151 | ; $s->teardown() | ||||||||||||||||||||||||||||
| 152 | ; $output | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ; *switch_to = sub{shift()->_run_runmode(@_)} | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | ; sub _run_runmode # __STEP must be 2 or 3 to run this | ||||||||||||||||||||||||||||
| 158 | { my ($s, $RM, @args) = @_ | ||||||||||||||||||||||||||||
| 159 | ; $s->__STEP < 2 && croak qq(Too early to call this method) | ||||||||||||||||||||||||||||
| 160 | ; $s->__STEP > 3 && croak qq(Too late to call this method) | ||||||||||||||||||||||||||||
| 161 | ; defined $RM && length $RM || croak qq(No run mode passed) | ||||||||||||||||||||||||||||
| 162 | ; $s->runmode = $RM # switch RM allowed just from here | ||||||||||||||||||||||||||||
| 163 | ; my $rm = $s->run_modes | ||||||||||||||||||||||||||||
| 164 | ; my $runmethod = $$rm{$RM} | ||||||||||||||||||||||||||||
| 165 | || $s->can($s->RM_prefix.$RM) && $s->RM_prefix.$RM | ||||||||||||||||||||||||||||
| 166 | || ($$rm{AUTOLOAD} && ++ my $al && $$rm{AUTOLOAD}) | ||||||||||||||||||||||||||||
| 167 | ; $^W && $al && carp qq(No run-method found for run mode "${\$s->runmode}" ) | ||||||||||||||||||||||||||||
| 168 | . qq(using run mode "AUTOLOAD") | ||||||||||||||||||||||||||||
| 169 | ; my $page | ||||||||||||||||||||||||||||
| 170 | ; if ( $runmethod ) | ||||||||||||||||||||||||||||
| 171 | { unshift @args, $RM if $al | ||||||||||||||||||||||||||||
| 172 | ; $page = $s->can($runmethod) | ||||||||||||||||||||||||||||
| 173 | ? $s->$runmethod( @args ) | ||||||||||||||||||||||||||||
| 174 | : eval{ $s->$runmethod( @args ) } | ||||||||||||||||||||||||||||
| 175 | ; $@ && croak qq(Error executing run mode "${\$s->runmode}": $@) | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | ; unless ( defined $s->page ) | ||||||||||||||||||||||||||||
| 178 | { $runmethod | ||||||||||||||||||||||||||||
| 179 | || croak qq(No run-method found for run mode "${\$s->runmode}") | ||||||||||||||||||||||||||||
| 180 | ; $s->page = $page | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | ; sub _send | ||||||||||||||||||||||||||||
| 185 | { my $s = shift | ||||||||||||||||||||||||||||
| 186 | ; $s->start_capture if $ENV{CGI_APP_RETURN_ONLY} # testing only | ||||||||||||||||||||||||||||
| 187 | ; print $s->query->${\$s->header_type}( %{$s->header_props} ) | ||||||||||||||||||||||||||||
| 188 | unless $s->header_type eq 'none' | ||||||||||||||||||||||||||||
| 189 | ; my $p = $s->page | ||||||||||||||||||||||||||||
| 190 | ; if ( ref $p eq 'CODE' ) | ||||||||||||||||||||||||||||
| 191 | { eval { $p->($s) } | ||||||||||||||||||||||||||||
| 192 | ; $@ && croak qq(Error executing the code referenced ) | ||||||||||||||||||||||||||||
| 193 | . qq(by run mode "${\$s->runmode}": $@) | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | elsif ( ref $p eq 'SCALAR' ) | ||||||||||||||||||||||||||||
| 196 | { print $$p | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | elsif ( not ref $p ) | ||||||||||||||||||||||||||||
| 199 | { print $p | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | ; $s->stop_capture if $ENV{CGI_APP_RETURN_ONLY} # testing only | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | ######### OLD CGI APP ############ | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | ; BEGIN | ||||||||||||||||||||||||||||
| 207 | { # useless but aliased for compatibility (support OO overriding) | ||||||||||||||||||||||||||||
| 208 | ; *QUERY = sub{shift()->query(@_)} | ||||||||||||||||||||||||||||
| 209 | ; *PARAMS = sub{shift()->param(@_)} | ||||||||||||||||||||||||||||
| 210 | ; *TMPL_PATH = sub{shift()->tmpl_path(@_)} | ||||||||||||||||||||||||||||
| 211 | ; *start_mode | ||||||||||||||||||||||||||||
| 212 | = *get_current_runmode = sub{shift()->runmode(@_)} | ||||||||||||||||||||||||||||
| 213 | ; *prerun_mode = sub{shift()->_run_runmode(@_)} | ||||||||||||||||||||||||||||
| 214 | ; *header_add = sub{shift()->header_props(@_)} | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | ######### JUST FOR TEST ############ | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | ; BEGIN # block needed just to allow testing | ||||||||||||||||||||||||||||
| 220 | { no strict 'refs' | ||||||||||||||||||||||||||||
| 221 | ; foreach my $n qw| dump dump_html load_tmpl | | ||||||||||||||||||||||||||||
| 222 | { *$n = sub | ||||||||||||||||||||||||||||
| 223 | { require CGI::Application::Plus::Util | ||||||||||||||||||||||||||||
| 224 | ; goto &{"CGI::Application::Plus::Util::$n"} | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | ######### CAPTURE ############ | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | ; my $output | ||||||||||||||||||||||||||||
| 232 | ; local *H | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | ; sub start_capture # starts to capture output | ||||||||||||||||||||||||||||
| 235 | { $output = '' | ||||||||||||||||||||||||||||
| 236 | ; *H = '*'.select() | ||||||||||||||||||||||||||||
| 237 | ; tie *H , 'CGI::Application::Plus::Capt' , \ $output | ||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | ; sub stop_capture # returns captured output | ||||||||||||||||||||||||||||
| 241 | { untie *H | ||||||||||||||||||||||||||||
| 242 | ; $output | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | ; package CGI::Application::Plus::Capt | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | ; sub TIEHANDLE | ||||||||||||||||||||||||||||
| 248 | { bless \@_, shift | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | ; sub PRINT | ||||||||||||||||||||||||||||
| 252 | { my $s = shift | ||||||||||||||||||||||||||||
| 253 | ; ${$$s[0]} .= join $,||'', map{defined $_? $_ : ''} @_ | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | ; 1 | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | __END__ | ||||||||||||||||||||||||||||