Regexp::Common OO interface

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.
 
J

Jim Keenan

A. Sinan Unur said:
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 confirm your results and your perplexity. Am I correct in thinking
that the results you expected were those from the 'operator' interface?

Jim Keenan
 
A

A. Sinan Unur

A. Sinan Unur said:
This script produces the following output:

D:\Home\htdocs\test.cgi> perl tv.pl
Using [ operator ]: ....
Using [ object_oriented ]: ....
Using [ subroutine_interface ]: ....

I would have expected the output to be identical in using all three
methods. I can't figure out why. Any ideas?

I confirm your results and your perplexity. Am I correct in thinking
that the results you expected were those from the 'operator' interface?

Jim Keenan

Thanks Jim. Your assumption is correct. At least now I know I am not the
only one :)

Sinan.
 
A

A. Sinan Unur

A. Sinan Unur ([email protected]) wrote on MMMML September
MCMXCIII in <URL:news:[email protected]>:
'' I would have expected the output to be identical in using all
'' three methods. I can't figure out why. Any ideas?

For starters, you have anchored the "operator" expression, while you
haven't done so with the OO or subroutine interfaces. So, it's quite
logical that you find matches for each value when using the subroutine
interface.

OK, that makes sense.
As for the OO interface, that's probably a bug, because I'd expect to
find matches in all cases as well. In the two years I've been
maintaining Regexp::Common, I haven't heard of a single use of OO
interface to the patterns of Regexp::Common, and only one or two
usages of sub routine interface. They are therefore hardly tested (it
would be a real pain in the ass to make tests due to the fact they
can't be anchored), so bugs are bound to be plenty.

So, I guess it would be prudent to stick with using the tied hash
interface.

Sinan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top