The following form, suggested by the book "Perl Best Practices" (
ISBN 0596001738), is easy to understand and maintain. If speed is truly important, it should be a candidate in your benchmark.
use strict;
use warnings;
sub string_type {
$_ = shift;
my $type
# format ? type
= /^\d{2}.\d{2}.\d{2}$/ ? 'date'
: /^\d{2}.\d{2}.\d{4}$/ ? 'date'
: /^[A-Z]{2}\d{9}[A-Z]{2}$/ ? 'Royal Mail Track & Trace code
+'
: /^\d{16}$/ ? 'visa card'
: /^\d{13}$/ ? 'EAN-13 barcode'
: 'unrecoginized string'
;
if ($type eq 'EAN-13 barcode') {
... # Handle sub-types
}
return $type;
}