sub ss_last_4 { my $n = shift; $n =~ tr/0-9//cd; # We can only work with numeric digits. # There are some basic constraints we can assure are met. die "$n cannot be a social security number.\n" if 9 != length($n) || $n =~ m/(^0{3})|(^\d{3}0{2})|(0{4}$)/ # A grouping of zeros represents a fictitious SS number. || substr($n,0,3) == 666 # Prefixes that == 666, or are >= 900 and <= 999 are reserved. || substr($n,0,3) >= 900; # Still alive, return the last four. return substr($n,-4,4); }