When you have "use warnings" enabled, then dont put "-w" on the #! line.
You can use the below code as a sort of boilerplate for your validation
activities as & when it requires some scaling in terms of checks on your
PANs/SSNs.
Also it's not clear what purpose is served by the first parameter, which
is a number, and not being used anywhere.
#!/usr/bin/perl
use v5.8.8; # or later
use strict;
use warnings;
use Carp qw( croak );
use File::Basename qw( basename );
use List::Util qw( max );
local $\ = qq{\n}; # auto-print newlines
local $, = qq{};
my $PN = basename $0; # program name
my $USAGE_MSG = <<"USAGE";
Usage: $PN Num_of_PANs PAN1 PAN2 ... PANn
Ex: $PN 3 AVDPS4325N AVMNP6543P DF45FGH7U
USAGE
my $VALID_PAN_REGEX = qr{
^ # Begin the pan string
[A-Z]{5} # 5 uppercase characters, followed by
[0-9]{4} # 4 numeric digits, and ending with a
[A-Z] # single uppercase character.
$ # End of the pan string
}x;
my %show_pan = (
gud => sub { "\33[32m[$_[0]]\33[0m" },
bad => sub { "\33[31m[$_[0]]\33[0m" },
);
use vars qw( *red );
local *red = $show_pan{bad};
my %Constraints = (
usage => sub {
croak
join "\n", (
red(qq{[ERROR] $PN: Incorrect Number of parameters.}),
qq{\tSupplied: '$_[0]'},
qq{\tRequired: atleast '2'.},
q{},
qq{$USAGE_MSG},
)},
non_empty => sub {
length($_[0]) > 0 or
croak
join "\n", (
red(qq{[ERROR] $PN: Empty first parameter.}),
qq{\tSupplied: '$_[0]'.},
qq{\tRequired: numeric.},
qq{},
qq{$USAGE_MSG},
)},
is_numeric => sub {
$_[0] =~ m/\D/ and
croak
join "\n", (
red(qq{[ERROR] $PN: Nonnumeric first parameter.}),
qq{\tSupplied: '$_[0]'.},
qq{\tRequired: numeric.},
qq{},
qq{$USAGE_MSG},
)},
);
sub validate_PANs {
my ($number_of_PANs, @PANs) = @_;
print "Initial number of PANs: $number_of_PANs.";
my $argc = @PANs;
my $fmt = max map { length() } @PANs;
my @dots = map { "." x ($fmt - length() + 3) } @PANs;
while ( defined(my $current_pan = shift @PANs) ) {
printf '%s: %s%s',
q{Validating the PAN inside the function},
qq{<$current_pan>},
$dots[$argc-$#PANs-2]
;
my $is_a_valid_PAN = $current_pan =~ m/$VALID_PAN_REGEX/;
my $result = $is_a_valid_PAN ? "YES" : "NO";
print $show_pan{ $is_a_valid_PAN ? "gud" : "bad" }->($result);
}
}
# validate constraints on the number of params supplied
$Constraints{usage}->(0+@ARGV)
if @ARGV < 2;
# validate constraints on the 1st param
$Constraints{$_}->($ARGV[0])
for qw( non_empty is_numeric );
validate_PANs( @ARGV );
__END__