Вероятно, вы бы предпочли иметь чек в виде подпрограммы
#!/usr/bin/perl
use strict;
use warnings;
my $debug = 0;
my @gst_numbers = <DATA>;
my $state_code = 35;
foreach my $gst_number (@gst_numbers) {
chomp $gst_number;
printf "%s %s\n",
$gst_number,
validate_gst($gst_number,$state_code)
? "valid" : "invalid";
}
sub validate_gst {
my $gst_number = shift;
my $state_code = shift;
my %code;
printf "%s : %s\n",
$gst_number, $state_code
if $debug;
my $gst_regex = qr/(\d{2})([A-Z\d]{10})(\d)(Z)([A-Z\d])/;
return 0 if length $gst_number < 15;
if( $gst_number =~ $gst_regex ) {
@code{qw(st pan d z x)} = ($1,$2,$3,$4,$5);
printf "ST: %s PAN: %s D: %s Z: %s X: %s\n",
$code{st}, # state code
$code{pan}, # PAN number
$code{d}, # state reg number
$code{z}, # Z
$code{x} # check code
if $debug;
return 0 if $code{st}+0 != $state_code; # state match?
return 0 if $code{pan} !~ /[A-Z]{5}\d{4}[A-Z\d]/; # PAN correct?
return 0 if $code{d} !~ /\d/; # state reg number?
return 0 if $code{z} !~ /Z/; # Z?
# check code?
return 1; # check passed
} else {
return 0; # check not passed
}
}
__DATA__
1234567890123Z5
35AABCS1429B1AX
121234567890DZD
37AABCS6428B1AX
35AABCS2439B1ZX
42AABCS1429B1AX
35AABCS1729B1ZX
Белый медведь