RegexpCommon
if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "e` un numero decimale\n" }
che e` abbastanza complessa da sembrare scritta dal nostro gatto magico. Riscriviamo lo stesso pezzo di codice con Regexp::Common:
use Regexp::Common;
if ( /$RE{num}{real}/ ) { print "e` un numero decimale\n" }
Immediatamente leggibile, e chiaro anche a chi non ha dimestichezza con le espressioni regolari. E' cosa che può tornare utile, in un progetto che coinvolge più persone con competenze diverse.
use strict;
use warnings;
use Data::Dumper;
use Regexp::Common 'pattern';
pattern name => [ qw/tel cell -operator/ ] ,
create => sub {
my $self = shift;
my $flags = shift;
my %prefix = (
Wind => [ 328 ] ,
Vodaphone => [ 348, 349 ] ,
Telecom => [ 338, 335 ] ,
Fake => [ 555 ]
);
my $pref = '';
if ( defined $flags->{ -operator } ) {
$pref = join "|", @{$prefix{$flags->{ -operator }}};
}
else {
$pref = join "|", map { @$_ } values %prefix;
}
return '^'
. "($pref)"
. '\d+$';
}
;
use Test::More qw/ no_plan /;
ok( "3492323298" =~ $RE{tel}{cell}{-operator => 'Vodaphone'} );
ok( "5553429844" =~ $RE{tel}{cell}{-operator => 'Fake'} );
ok( "3383429844" =~ $RE{tel}{cell} );
[larsen]
|