Ask a Question related to PERL Beginners, Design and Development.
-
Gary #1
subroutine parameter validation
Hi folks,
I've had a go a rolling my own subroutine validation code, and would welcome
any constructive comments on it.
My aim was to allow existing code to continue working using positional params,
as well as allowing varieties of -key=>value style arguments.
Gary
#!/usr/bin/perl -w
use strict;
my %_DEBUG=('new'=>0,'mysub'=>1);
my %_PARAMS=('mysub'=>'fred:s:others:a:');
# return debug info
sub debug {
my $caller=(caller(1))[3];
$caller=~s/^.*:://;
return (defined $_DEBUG{$caller}) ? $_DEBUG{$caller} : 0;
};
sub validate {
# work our who we are and what we want
my $caller=(caller(1))[3];
$caller=~s/^.*:://;
unless (defined $_PARAMS{$caller}) {
warn "validate: unknown routine $caller\n";
return undef;
}
my %resp=(); # hash to return
# now validate parameters
my $type=ref $_[0];
if ($type) { # hashref or arrayref
%resp=%{$_[0]};
} elsif ($_[0]=~/^-/) { # standard comma or arrow syntax
%resp=@_;
} else { # positional array passing
my @params=split(/:/,$_PARAMS{$caller});
while (@_) {
my $key=shift @params; # param name
my $type=shift @params; # string or array
if ($type eq 's') { # single value
$resp{$key}=shift @_;
} else { # handle array
if (ref $_[0]) { # hashref or arrayref
$resp{$key}=shift;
} else { # array
@{$resp{$key}}=@_; # copy array to arrayref
$#_=-1; # then emty original array
}
}
} # while @_
}
foreach my $key (keys %resp) {
if ($key=~/^-(.*)$/) { # remove dash
$resp{$1}=$resp{$key}; # create correct key
delete $resp{$key}; # delete wrong one
}
}
return %resp;
};
sub mysub {
print "starting 'mysub'.\n" if (&debug);
my %resp=validate(@_);
return 0 unless (%resp);
foreach my $key (keys %resp) {
print "$key=$resp{$key}\n";
}
return 1;
}
print "1\n";
print "returned okay\n" if mysub(-fred=>'Ginger',-others=>{-ginger=>'Fred'});
print "2\n";
print "returned okay\n" if mysub(-fred,'Ginger',-others,'Fred');
print "3\n";
print "returned okay\n" if
mysub({-fred=>'Ginger',-others=>{-ginger=>'Fred'}});
print "4\n";
print "returned okay\n" if mysub(-fred=>'Ginger',-others=>{-ginger=>'Fred'});
--
Gary Stainburn
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
Gary Guest
-
CFFORM validation trumping Custom Validation
Is there any way for custom form validation to work in concert with the cfform validation? I have a custom script that compares the values of two... -
CFFORM Validation trumping Custom Form Validation
Is there any way for custom form validation to work in concert with the cfform validation? I have a custom script that compares the values of two... -
Date Parameter For Saved Parameter Queries
Hi again, I finally got to using saved parameter queries in my application (a big thank you to Bob Barrows for helping me with this). Currently... -
validation summary doesnt display when there's client-side validation
I have a custom validator that validates a numeric field, txtField, that allows for thousand separators. I also placed a validation summary so... -
only custom validation control does server side validation?
On a CustomValidator you have to provide the validation code because otherwise it doesn't know what to do for the validation. Other validator...



Reply With Quote

