I carefully read your code and made a few tweaks to it. You are making a good start. Read and reread the docs, ask questions and keep working at it. I strongly recommend that you take the time to understand regexes. They are a very, very, useful and powerful tool.
I noticed some problems with your code as I read it.
- You have inconsistent use of inclusive vs exclusive comparisons.
- Your algorithm for floating point validation would allow numbers like '12. 3' (that's a space between the . and the 3) to pass.
- There are a number of variables that seem to come from nowhere. Are they globals?
- You are using C style for loops.
For loops in perl are usually done as follows:
# You had
for ($l_count = 1; $l_count < $l_len; $l_count++)
{
$lower=$lower."9";
}
# Try this instead:
for my $l_count ( 1..$l_len ) {
$lower=$lower."9";
}
# To build up a string like this you should be using the 'x' operator
my $lower = '9' x $l_len;
Here's your code with my tweaks applied. What follows is untested, but should work.
use constant MATCH => 1; # Return value for good match
use constant FAIL => 0; # Retrun value for failed match
use constant MIN => 0; # Index of minimum value for integer checks
use constant MAX => 1; # Index of maximum value for integer checks
############# SUB-ROUTINE TO CHECK FOR INTEGER TYPE ##############
#check for small int, big int and int data types and return a 0 if cor
+rect
# else return a 1
sub int_chk {
my $input = shift;
my $data_type = shift; # Where was this coming from in your code?
$input = $input+0;
# Test for integerness
if ( $input != int( $input ) ) {
return FAIL;
}
# Use a hash lookup to simplify your code.
my %check = ( # type MIN
+ MAX
SMALLINT => [ -32768, 32767 ],
INTEGER => [ -2147483648, 2147483647 ],
BIGINT => [ -9223372036854770000, 9223372036854770000 ],
);
# Test for range
if ( exists $check{$data_type} ) {
my $match = (
$input >= $check{$data_type}[MIN] # I used inclusive range
+s here
and
$input <= $check{$data_type}[MAX]
) ? MATCH : FAIL;
return $match;
}
else {
return FAIL;
}
die "Unreachable code executed";
}
sub decimal_chk {
my $input = shift;
my $whole_places = shift || 3; # Set a default value
my $decimal_places = shift || 2; # for each of these args
return $input =~ /
^ # Start of input
-? # Optional minus sign
\d{0,$whole_places} # 1-? whole number digits
(
\. # Manadatory decimal point
\d{0,$decimal_places} # 1-? decimal places
)? # decimal section is optional
$ # End of input
/x ? MATCH : FAIL ; # x modifier allows comments and whitespac
+e
}
|