Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Set::Range - conditional testing on sets by japhy
in thread Set::Range - conditional testing on sets by $code or die

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-18 00:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found