| 1 | # -*- Mode: perl -*- | ||
| 2 | # | ||
| 3 | # $Id: Utils.pm,v 0.1 2001/04/22 17:57:03 ram Exp $ | ||
| 4 | # | ||
| 5 | # Copyright (c) 1998-2001, Raphael Manfredi | ||
| 6 | # Copyright (c) 2000-2001, Christophe Dehaudt | ||
| 7 | # | ||
| 8 | # You may redistribute only under the terms of the Artistic License, | ||
| 9 | # as specified in the README file that comes with the distribution. | ||
| 10 | # | ||
| 11 | # HISTORY | ||
| 12 | # $Log: Utils.pm,v $ | ||
| 13 | # Revision 0.1 2001/04/22 17:57:03 ram | ||
| 14 | # Baseline for first Alpha release. | ||
| 15 | # | ||
| 16 | # $EndLog$ | ||
| 17 | # | ||
| 18 | |||
| 19 | # | ||
| 20 | # That package is a collection of routines dedicated for the input | ||
| 21 | # treatment. They are intend to be used by the screen designer when | ||
| 22 | # creating a Form::Field object by using the record_field method of a | ||
| 23 | # Screen. Its scope is limited to: | ||
| 24 | # | ||
| 25 | # - validate the screen fields (-verify parameter). The methods in | ||
| 26 | # the set for the validation purpose are normally named 'is_xxx' | ||
| 27 | # (ex: is_num). All take as the first parameter the value to | ||
| 28 | # verify and return either an error message when error or 0. | ||
| 29 | # | ||
| 30 | # - patch the input value (-patch parameter). As validation | ||
| 31 | # routines, they are normally named 'xxx2xxx' (ex: | ||
| 32 | # float2int). All take as the first argument the value to patch | ||
| 33 | # and return the value patched or not. | ||
| 34 | # | ||
| 35 | # Routine naming convention is just a recommendation. | ||
| 36 | # | ||
| 37 | # As the set is certainly not complete, there is way to allow the user | ||
| 38 | # to make it larger by registering his own routines (use | ||
| 39 | # CGI::Mxscreen->add_utils_path for that). The routines that will be | ||
| 40 | # later defined must follow the signature principles (takes the value | ||
| 41 | # as the first argument and return the correct code schema) according | ||
| 42 | # to its goal (validation or patch). | ||
| 43 | # | ||
| 44 | use strict; | ||
| 45 | |||
| 46 | package CGI::MxScreen::Form::Utils; | ||
| 47 | |||
| 48 | use Carp::Datum; | ||
| 49 | use Log::Agent; | ||
| 50 | |||
| 51 | ############################################################################# | ||
| 52 | # # | ||
| 53 | # Validation routines # | ||
| 54 | # # | ||
| 55 | ############################################################################# | ||
| 56 | |||
| 57 | # | ||
| 58 | # ::is_num | ||
| 59 | # | ||
| 60 | sub is_num { | ||
| 61 | DFEATURE(my $f_); | ||
| 62 | my ($v) = @_; | ||
| 63 | |||
| 64 | return DVAL 0 if ($v + 0) eq $v; | ||
| 65 | return DVAL "must be a numerical value"; | ||
| 66 | } | ||
| 67 | |||
| 68 | # | ||
| 69 | # ::is_greater | ||
| 70 | # | ||
| 71 | sub is_greater { | ||
| 72 | DFEATURE(my $f_); | ||
| 73 | my ($v, $boundary) = @_; | ||
| 74 | |||
| 75 | # check that it is a num | ||
| 76 | my $ret = is_num($v); | ||
| 77 | return DVAL $ret if $ret; | ||
| 78 | return DVAL 0 if $v > $boundary; | ||
| 79 | return DVAL "must be greater than $boundary"; | ||
| 80 | } | ||
| 81 | |||
| 82 | ############################################################################# | ||
| 83 | # # | ||
| 84 | # Patching routines # | ||
| 85 | # # | ||
| 86 | ############################################################################# | ||
| 87 | |||
| 88 | # | ||
| 89 | # ::float2int | ||
| 90 | # | ||
| 91 | sub float2int { | ||
| 92 | DFEATURE(my $f_); | ||
| 93 | my ($v) = @_; | ||
| 94 | |||
| 95 | if ($v =~ /^\s*(-?\d+)(?:\.\d+)?\s*/) { | ||
| 96 | return DVAL $1; | ||
| 97 | } | ||
| 98 | |||
| 99 | return DVAL $v; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | ############################################################################# | ||
| 104 | # # | ||
| 105 | # routines dedicated for the utilities management # | ||
| 106 | # FOR INTERNAL PURPOSE ONLY. # | ||
| 107 | # DO NOT USE AS -verify or -patch PARAMETER # | ||
| 108 | # # | ||
| 109 | ############################################################################# | ||
| 110 | |||
| 111 | use vars qw(@UTILS_PATH %UTILS_CACHE); | ||
| 112 | @UTILS_PATH = ('CGI::MxScreen::Form::Utils'); | ||
| 113 | |||
| 114 | # | ||
| 115 | # ::add_utils_path | ||
| 116 | # | ||
| 117 | # prepend the given package to the utils path | ||
| 118 | # | ||
| 119 | sub add_path { | ||
| 120 | DFEATURE my $f_; | ||
| 121 | |||
| 122 | unshift @UTILS_PATH, @_; | ||
| 123 | %UTILS_CACHE = (); # Changing path invalidates caching | ||
| 124 | |||
| 125 | return DVOID; | ||
| 126 | } | ||
| 127 | |||
| 128 | no strict qw(refs); | ||
| 129 | |||
| 130 | # | ||
| 131 | # ::lookup | ||
| 132 | # | ||
| 133 | # lookup for a routine into the list of packages. The value is cached | ||
| 134 | # to benefit of the next access | ||
| 135 | # | ||
| 136 | # Arguments: | ||
| 137 | # $routine: string | ||
| 138 | # | ||
| 139 | # Return: | ||
| 140 | # a code reference to the routine or undef when not found | ||
| 141 | # | ||
| 142 | sub lookup { | ||
| 143 | DFEATURE my $f_; | ||
| 144 | my ($routine) = @_; | ||
| 145 | |||
| 146 | # first try with the local cache | ||
| 147 | return DVAL $UTILS_CACHE{$routine} if | ||
| 148 | defined $UTILS_CACHE{$routine}; | ||
| 149 | |||
| 150 | # maybe the routine was given with an absolute path? | ||
| 151 | if ($routine =~ /::/ && defined &$routine) { | ||
| 152 | $UTILS_CACHE{$routine} = \&$routine; | ||
| 153 | return DVAL $UTILS_CACHE{$routine}; | ||
| 154 | } | ||
| 155 | |||
| 156 | # really look for the routine in all the package path | ||
| 157 | for my $pkg (@UTILS_PATH) { | ||
| 158 | logdbg 'info', "looking for routine $pkg\:\:$routine"; | ||
| 159 | next unless defined &{"$pkg\:\:$routine"}; | ||
| 160 | $UTILS_CACHE{$routine} = \&{"$pkg\:\:$routine"}; | ||
| 161 | return DVAL $UTILS_CACHE{$routine}; | ||
| 162 | } | ||
| 163 | |||
| 164 | return DVAL undef; | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; | ||
| 168 | |||
| 169 | =head1 NAME | ||
| 170 | |||
| 171 | CGI::MxScreen::Form::Utils - Standard validation & patching routines | ||
| 172 | |||
| 173 | =head1 SYNOPSIS | ||
| 174 | |||
| 175 | # Not meant to be used directly | ||
| 176 | |||
| 177 | =head1 DESCRIPTION | ||
| 178 | |||
| 179 | This module is the standard namespace for validation and patching routines. | ||
| 180 | See L<CGI::MxScreen::Form::Field> to learn how to link such routines | ||
| 181 | to fields. | ||
| 182 | |||
| 183 | The following standard validation routines are provided: | ||
| 184 | |||
| 185 | =over 4 | ||
| 186 | |||
| 187 | =item C<is_num> | ||
| 188 | |||
| 189 | Check that field holds a number. | ||
| 190 | |||
| 191 | =item C<is_greater> I<value> | ||
| 192 | |||
| 193 | Check that field is greater than supplied value. | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | The following standard patching routines are provided: | ||
| 198 | |||
| 199 | =over 4 | ||
| 200 | |||
| 201 | =item C<float2int> | ||
| 202 | |||
| 203 | Removes fractional part of number. | ||
| 204 | |||
| 205 | =back | ||
| 206 | |||
| 207 | =head1 AUTHORS | ||
| 208 | |||
| 209 | The original authors are | ||
| 210 | Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>> | ||
| 211 | and | ||
| 212 | Christophe Dehaudt F<E<lt>Christophe.Dehaudt@teamlog.frE<gt>>. | ||
| 213 | |||
| 214 | Send bug reports, suggestions, problems or questions to | ||
| 215 | Jason Purdy F<E<lt>Jason@Purdy.INFOE<gt>> | ||
| 216 | |||
| 217 | =head1 SEE ALSO | ||
| 218 | |||
| 219 | CGI::MxScreen::Form::Field(3). | ||
| 220 | |||
| 221 | =cut | ||
| 222 |