| 1 | package Class::CGI::Handler; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | Class::CGI::Handler - Base class for Class::CGI handlers | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | Version 0.20 | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.20'; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | use base 'Class::CGI::Handler'; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub handle { | ||||||||||||||||||||
| 23 | my $self = shift; | ||||||||||||||||||||
| 24 | my $cgi = $self->cgi; | ||||||||||||||||||||
| 25 | my $param = $self->param; | ||||||||||||||||||||
| 26 | # validate stuff and return anything you want | ||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | Handlers for C<Class::CGI> should inherit from this class. This class | ||||||||||||||||||||
| 32 | provides a constructor which builds the handler object and checks to see if | ||||||||||||||||||||
| 33 | the param value from the CGI data is required. If so, it will automatically | ||||||||||||||||||||
| 34 | set a "missing" error if the parameter is not present. See the L<has_param> | ||||||||||||||||||||
| 35 | method for more details. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | ############################################################################## | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 Methods | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =head2 new | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | my $handler = Some::Handler::Subclass->new( $cgi, $param ); | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | Returns a new handler object. Returns nothing if the parameter is required | ||||||||||||||||||||
| 48 | but not present. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 53 | 2 | my ( $class, $cgi, $param ) = @_; | |||||||||||||||||||
| 54 | 2 | my $self = bless { | |||||||||||||||||||
| 55 | cgi => $cgi, | ||||||||||||||||||||
| 56 | param => $param, | ||||||||||||||||||||
| 57 | }, $class; | ||||||||||||||||||||
| 58 | 2 | ▶ | if ( $cgi->is_required($param) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 2 | ▶ | return unless $self->has_param; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 1 | return $self->handle; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | ############################################################################## | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =head2 has_param | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | if ( $handler->has_param ) { | ||||||||||||||||||||
| 69 | ... | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | Returns a boolean value indicating whether or not the current parameter was | ||||||||||||||||||||
| 73 | found in the form. If a parameter is "real", that is to say, the requested | ||||||||||||||||||||
| 74 | parameter name and the actual parameter name are identical, then this method | ||||||||||||||||||||
| 75 | should be all you need. For example: | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | In the HTML: | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | <input type="text" name="age"/> | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | In the code: | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | my $age = $cgi->param('age'); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | If the parameter is "virtual" (the requested parameter name does not match the | ||||||||||||||||||||
| 86 | name in the HTML), then this method should be overridden in your subclass. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | Note that the this method will automatically report the parameter as "missing" | ||||||||||||||||||||
| 89 | to the C<Class::CGI> object if it's a required parameter. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | ▶ | sub has_param { | ||||||||||||||||||
|
Subroutine
has_param: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 94 | 2 | my $self = shift; | |||||||||||||||||||
| 95 | 2 | my $param = $self->param; | |||||||||||||||||||
| 96 | 2 | ▶ | return 1 unless $self->_missing($param); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 1 | $self->cgi->add_missing($param); | |||||||||||||||||||
| 98 | 1 | return; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | ############################################################################## | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head2 has_virtual_param | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | if ( $cgi->has_virtual_param( $param, @list_of_parameters ) ) { | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Very similar to the C<has_param> method. However, instead of checking to see | ||||||||||||||||||||
| 109 | if the current parameter exists, you pass in the name of the virtual parameter | ||||||||||||||||||||
| 110 | and a list of the component parameters which comprise the virtual parameter. | ||||||||||||||||||||
| 111 | For example: | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | if ( $handler->has_virtual_param( 'date', qw/day month year/ ) ) { | ||||||||||||||||||||
| 114 | .... | ||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Note that the this method will automatically report the parameter as "missing" | ||||||||||||||||||||
| 118 | to the C<Class::CGI> object if it's a required parameter. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 2 | ▶ | sub has_virtual_param { | ||||||||||||||||||
|
Subroutine
has_virtual_param: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 123 | 2 | my ( $self, $param, @components ) = @_; | |||||||||||||||||||
| 124 | 2 | ▶ | if ( my %missing = $self->_missing(@components) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 1 | my @missing = grep { exists $missing{$_} } @components; | |||||||||||||||||||
| 126 | 1 | $self->cgi->add_missing( | |||||||||||||||||||
| 127 | $param, | ||||||||||||||||||||
| 128 | "The '$param' is missing values for (@missing)" | ||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||
| 130 | 1 | return; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 1 | return 1; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | ############################################################################## | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head2 handle | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | return $handler->handle; | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | This method must be overridden in a subclass. It is the primary method used | ||||||||||||||||||||
| 142 | to actually validate and optionally untaint form data and return the | ||||||||||||||||||||
| 143 | appropriate data. See C<WRITING HANDLERS> in the L<Class::CGI> documentation. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub handle { | ||||||||||||||||||
|
Subroutine
handle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 148 | 0 | require Carp; | |||||||||||||||||||
| 149 | 0 | Carp::croak("You must override the Class::CGI::handle() method"); | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | ############################################################################## | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head2 cgi | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | my $cgi = $handler->cgi; | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Returns the C<Class::CGI> object used to call the handler. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 8 | ▶ | sub cgi { shift->{cgi} } | ||||||||||||||||||
|
Subroutine
cgi: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | ############################################################################## | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head2 param | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | my $param = $cgi->param; | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Returns the parameter name the user has requested. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 4 | ▶ | sub param { shift->{param} } | ||||||||||||||||||
|
Subroutine
param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | ############################################################################## | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 _missing | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | if ( my %missing = $handler->_missing(@params) ) { | ||||||||||||||||||||
| 181 | ... | ||||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | This is a protected method which should only be called by subclasses. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Given a list of parameter names (actual, not virtual), this method will return | ||||||||||||||||||||
| 187 | a hash of all parameters whose value is undefined or the empty string. The | ||||||||||||||||||||
| 188 | keys are the parameter names and the values are the value received from the | ||||||||||||||||||||
| 189 | C<Class::CGI> object. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 4 | ▶ | sub _missing { | ||||||||||||||||||
|
Subroutine
_missing: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 194 | 4 | my ( $self, @params ) = @_; | |||||||||||||||||||
| 195 | 4 | my $cgi = $self->cgi; | |||||||||||||||||||
| 196 | my %missing = | ||||||||||||||||||||
| 197 | 2 | map { $_->[0], $_->[1] } # prevent the "odd number of elements" warning | |||||||||||||||||||
| 198 | 7 | ▶ | grep { !defined $_->[1] || '' eq $_->[1] } | ||||||||||||||||||
|
Condition66%2/3
!defined($_->[1]) || '' eq $_->[1]
MC/DC50%1/2
!defined($_->[1]) || '' eq $_->[1]
!defined($_->[1])
'' eq $_->[1]
Truth table66%2/3
not(A) || B
A= !defined($_->[1])B= '' eq $_->[1]
| |||||||||||||||||||||
| 199 | 4 | map { [ $_, $cgi->raw_param($_) ] } @params; | |||||||||||||||||||
| 200 | 4 | return %missing; | |||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head1 TODO | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | This module should be considered alpha code. It probably has bugs. Comments | ||||||||||||||||||||
| 206 | and suggestions welcome. | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | The only current "TODO" is to allow overridding error messages. | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | =head1 AUTHOR | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | Curtis "Ovid" Poe, C<< <ovid@cpan.org> >> | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head1 SUPPORT | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | There is a mailing list at L<http://groups.yahoo.com/group/class_cgi/>. | ||||||||||||||||||||
| 217 | Currently it is low volume. That might change in the future. | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =head1 BUGS | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | Please report any bugs or feature requests to | ||||||||||||||||||||
| 222 | C<bug-class-cgi@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||
| 223 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-CGI>. | ||||||||||||||||||||
| 224 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||
| 225 | your bug as I make changes. | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | If you are unsure if a particular behavior is a bug, feel free to send mail to | ||||||||||||||||||||
| 228 | the mailing list. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | Copyright 2006 Curtis "Ovid" Poe, all rights reserved. | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 235 | under the same terms as Perl itself. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 2 | 1; | |||||||||||||||||||