A
A. Sinan Unur
Hello All:
I am a little confused and would appreciate another set of eyes looking at
the following script and telling me what I am not getting:
#! perl
use strict;
use warnings;
use Regexp::Common qw(number RE_num_real);
my %validators = (
operator => sub {
my $v = shift;
if( $v =~ /^$RE{num}{real}{-keep}{-places => '0,2'}$/ ) {
print "matched!\n";
return $1;
}
return;
},
object_oriented => sub {
my $v = shift;
if( $RE{num}{real}{-keep}{-places => '0,2'}->matches($v) ) {
print "matched!\n";
return $1;
}
return;
},
subroutine_interface => sub {
my $v = shift;
if( $v =~ RE_num_real(-keep => 1, -places => '0,2') ) {
print "matched!\n";
return $1;
}
return;
},
);
my @values = qw(1 1.1 1.11 1.111);
for my $v (keys %validators) {
print "Using [ $v ]:\n";
for my $value (@values) {
if(defined (my $validated = $validators{$v}->($value))) {
print "$value\t: validated\n";
} else {
print "$value\t: not validated\n";
}
}
print "---\n";
}
__END__
This script produces the following output:
D:\Home\htdocs\test.cgi> perl tv.pl
Using [ operator ]:
matched!
1 : validated
matched!
1.1 : validated
matched!
1.11 : validated
1.111 : not validated
---
Using [ object_oriented ]:
matched!
1 : not validated
1.1 : not validated
1.11 : not validated
1.111 : not validated
---
Using [ subroutine_interface ]:
matched!
1 : validated
matched!
1.1 : validated
matched!
1.11 : validated
matched!
1.111 : validated
---
I would have expected the output to be identical in using all three
methods. I can't figure out why. Any ideas?
Thanks.
Sinan.
I am a little confused and would appreciate another set of eyes looking at
the following script and telling me what I am not getting:
#! perl
use strict;
use warnings;
use Regexp::Common qw(number RE_num_real);
my %validators = (
operator => sub {
my $v = shift;
if( $v =~ /^$RE{num}{real}{-keep}{-places => '0,2'}$/ ) {
print "matched!\n";
return $1;
}
return;
},
object_oriented => sub {
my $v = shift;
if( $RE{num}{real}{-keep}{-places => '0,2'}->matches($v) ) {
print "matched!\n";
return $1;
}
return;
},
subroutine_interface => sub {
my $v = shift;
if( $v =~ RE_num_real(-keep => 1, -places => '0,2') ) {
print "matched!\n";
return $1;
}
return;
},
);
my @values = qw(1 1.1 1.11 1.111);
for my $v (keys %validators) {
print "Using [ $v ]:\n";
for my $value (@values) {
if(defined (my $validated = $validators{$v}->($value))) {
print "$value\t: validated\n";
} else {
print "$value\t: not validated\n";
}
}
print "---\n";
}
__END__
This script produces the following output:
D:\Home\htdocs\test.cgi> perl tv.pl
Using [ operator ]:
matched!
1 : validated
matched!
1.1 : validated
matched!
1.11 : validated
1.111 : not validated
---
Using [ object_oriented ]:
matched!
1 : not validated
1.1 : not validated
1.11 : not validated
1.111 : not validated
---
Using [ subroutine_interface ]:
matched!
1 : validated
matched!
1.1 : validated
matched!
1.11 : validated
matched!
1.111 : validated
---
I would have expected the output to be identical in using all three
methods. I can't figure out why. Any ideas?
Thanks.
Sinan.