Here's my implementation. Documentation will come in a while, and I haven't tested it (I just wrote it, and I'm in the mood for dinner), but here's the code:
package Set::Range;
$VERSION = '1.0j'; # j for japhy ;)
sub new {
my $class = shift;
my $self = bless {
CACHE => {},
RANGE => [],
}, $class;
while (my ($k,$v) = splice(@_, 0, 2)) {
my ($low, $high) = split /\s*(?:,|\.{2,})\s*/, $k;
unless ($high) {
$high = substr($low, 1);
chop $low;
}
my $li = (substr($low, 0, 1, '') eq '[');
my $hi = (substr($high, 0, 1, '') eq ']');
my $l = $self->fmt($low);
my $h = $self->fmt($high);
$self->{CACHE}{$l} = $v if $li;
$self->{CACHE}{$h} = $v if $hi;
push @{ $self->{RANGE} }, {
LO => $l,
HI => $h,
LO_INC => $li,
HI_INC => $hi,
VALUE => $v,
CODE =>
($l == $h && sub { $l == $_[0] })
||
($li && $hi && sub { ($l ne '' && $l <= $_[0]) and ($h ne '' &
+& $_[0] <= $h) } )
||
($li && sub { ($l ne '' && $l <= $_[0]) and ($h ne '' && $_[0]
+ < $h) } )
||
($hi && sub { ($l ne '' && $l < $_[0]) and ($h ne '' && $_[0]
+<= $h) } )
||
sub { ($l ne '' && $l < $_[0]) and ($h ne '' && $_[0] < $h) }
};
}
return $self;
}
sub range {
my ($self, $value) = @_;
$value = $self->fmt($value);
return $self->{CACHE}{$value} if exists $self->{CACHE}{$value};
$_->{CODE}->($value) and return($self->{CACHE}{$value} = $_->{VALUE}
+)
for @{ $self->{RANGE} };
return;
}
package Set::Range::Date;
@ISA = qw( Set::Range );
sub fmt {
my $d = pop;
return sprintf "%04d%02d%02d",
($d =~ /(\d{1,2})\D*(\d{1,2})\D*(\d+)/)[2,1,0];
}
package Set::Range::Date::Eu;
@ISA = qw( Set::Range );
sub fmt {
my $d = pop;
return sprintf "%04d%02d%02d",
($d =~ /(\d{1,2})\D*(\d{1,2})\D*(\d+)/)[2,0,1];
}
package Set::Range::Time;
@ISA = qw( Set::Range );
sub fmt {
my $t = pop;
# assume it's a timestamp
return $t unless $t =~ /\D/;
# does it look a like 12-hour clock?
if ($t =~ /([apAP])/) {
my $is_pm = (lc($1) ne 'a');
my @parts = $t =~ /(\d+)/g;
$parts[0] %= 12;
$parts[0] += 12 if $is_pm;
return sprintf '%02d' x 4, @parts;
}
# otherwise, assume 24-hour clock
return sprintf '%02d' x 4, $t =~ /(\d+)/g;
}
package Set::Range::Number;
@ISA = qw( Set::Range );
sub fmt { pop }
1;
__END__
Sample use is:
use Set::Range;
my $r = Set::Range::Date->new(
'[1/1/2001,12/31/2001]' => "",
'[,1/1/2001)' => "before ",
'(12/31/2001,]' => "after ",
);
print "Today is ", $r->range('5/6/2000'), "now";
Mine uses standard set notation (a bracket means inclusive, and a parenthesis means exclusive) as the key, and some value (such as a code reference) for the value. The sets should be sent in a meaningful order -- the order they'll be tested in.
I also use OO design to specify the formatting as a sub-class action, and leave the actual comparing up to the main class.
This isn't meant to be a challenge or whatever to your module, it's just how I would write the thing. Yours looks rather nice (save that
eval(), which you tell me you've gotten rid of in your CPAN'd version).
japhy --
Perl and Regex Hacker