Coding Domain

Perl Modules: Test::Input


Downloads
Perl Modules
Name
Test::Input - Tests for input validation

Synopsis
use Test::Input;

print "A number" if(IsNumber($N));
print "In Range" if(InRange($N, 1, 10));
print "Is a URL" if(IsURL($URL));
print "Mailaddr" if(isEmail($Address));

# Console tool
my ($PASS1, $PASS2) = GetInput
{
  return "Password don't match!" if($_[0] ne $_[1]);
  return "Password length needs to be at lease 8 characters!" if(length($_[0]) < 8);
}
'Choose Password'     => undef,
'Re-enter Password'   => undef;

print "Password is $PASS1, retyped as $PASS2";

Description
This module exports testing functions that can be used to validate the user's input.

Exported Functions
IsNumber(Variable)
Returns True if the input is a number

InRange(Number, Min, Max)
Returns True if the input is a number, and it lies in the range specified by Min and Max.

IsURL(Location)
Tests whether the input is a reasonable webpage URL or not.

IsEmail(Address)
Tests if the e-mail address is an normal address. This regular expression is based on the one found in the book "CGI Programming with Perl", by O'Reilly. Unlike the regular expression used in the book "Mastering Regular Expressions", by Jeffy Friedl, this subroutine does not accept all e-mail addresses allowed by the SMTP specification. That specification also allows these addresses, which includes some comments or group names:

Alfred Neuman <Neuman@BBN-TENEXA>
":sysmail" @ Some-Group . Some-Org
Nuhammed.(I am the Greatest) Ali @(the)Vegas.WBA

That's not an error of the author, he never intended that that regexp would be used to validate e-mail addresses at the internet. It's just an experiment if such a regular expression could be written.

Properly, you don't want to accept any of those e-mail addresses in your input. This routine allows all commonly used characters, a domain name followed by an extension of 2,3 or 4 characters. It also accepts an IP address instead of domain names.

GetInput(&CallbackTest, $InputPrompt1 => $InputPrompt1Default, $InputPromptN => $InputPromptNDefault)
Retreives input from the user (in a console application). The first argument referrers to a callback function (=function pointer), the other arguments are strings containing the prompt to display, followed by a default value (or undef). The callback function is expected to test the input and return undef on success. If some if the input is not valid, a string containing an error message should be retured. When an error message is returned, the prompt will automatically display again and ask for the input.

Author
Copyright (c) 2001, Diederik van der Boor - All Rights Reserved