<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright 2001 Abhijit Menon-Sen &lt;ams@toroid.org&gt;

package Set::Crontab;

use strict;
use Carp;
use vars qw( $VERSION );

$VERSION = '1.03';

sub _expand
{
    my (@list, @and, @not);
    my ($self, $spec, $range) = @_;

    # 1,2-4,*/3,!13,&gt;9,&lt;15
    foreach (split /,/, $spec) {
        my @pick;
        my $step = $1 if s#/(\d+)$##;

        # 0+"01" == 1
        if    (/^(\d+)$/)       { push @pick, 0+$1;          }
        elsif (/^\*$/)          { push @pick, @$range;       }
        elsif (/^(\d+)-(\d+)$/) { push @pick, 0+$1..0+$2;    } 
        elsif (/^!(\d+)$/)      { push @not,  "\$_ != 0+$1"; }
        elsif (/^([&lt;&gt;])(\d+)$/) { push @and,  "\$_ $1 0+$2"; }

        if ($step) {
            my $i;
            @pick = grep { defined $_ if $i++ % $step == 0 } @pick;
        }

        push @list, @pick;
    }

    if (@and) {
        my $and = join q{ &amp;&amp; }, @and;
        push @list, grep { defined $_ if eval $and } @$range;
    }

    if (@not) {
        my $not = join q{ &amp;&amp; }, @not;
        @list = grep { defined $_ if eval $not } (@list ? @list : @$range);
    }

    @list = sort { $a &lt;=&gt; $b } @list;
    return \@list;
}

sub _initialise
{
    my ($self, $spec, $range) = @_;
    return undef unless ref($self);

    croak "Usage: ".__PACKAGE__."-&gt;new(\$spec, [\@range])"
        unless defined $spec &amp;&amp; ref($range) eq "ARRAY";

    $self-&gt;{LIST} = $self-&gt;_expand($spec, $range);
    $self-&gt;{HASH} = {map {$_ =&gt; 1} @{$self-&gt;{LIST}}};

    return $self;
};

sub new
{
    my $class = shift;
    my $self  = bless {}, ref($class) || $class;
    return $self-&gt;_initialise(@_);
}

sub contains
{
    my ($self, $num) = @_;

    croak "Usage: \$set-&gt;contains(\$num)" unless ref($self) &amp;&amp; defined $num;
    return exists $self-&gt;{HASH}{$num};
}

sub list
{
    my $self = shift;

    croak "Usage: \$set-&gt;list()" unless ref($self);
    return @{$self-&gt;{LIST}};
}

1;
__END__

=head1 NAME

Set::Crontab - Expand crontab(5)-style integer lists

=head1 SYNOPSIS

$s = Set::Crontab-&gt;new("1-9/3,&gt;15,&gt;30,!23", [0..30]);

if ($s-&gt;contains(3)) { ... }

=head1 DESCRIPTION

Set::Crontab parses crontab-style lists of integers and defines some
utility functions to make it easier to deal with them.

=head2 Syntax

Numbers, ranges, *, and step values all work exactly as described in
L&lt;crontab(5)&gt;. A few extensions to the standard syntax are described
below.

=over 4

=item &lt; and &gt;

&lt;N selects the elements smaller than N from the entire range, and adds
them to the set. &gt;N does likewise for elements larger than N.

=item !

!N excludes N from the set. It applies to the other specified 
range; otherwise it applies to the specified ranges (i.e. "!3" with a
range of "1-10" corresponds to "1-2,4-10", but "&gt;3,!7" in the same range
means "4-6,8-10").

=back

=head2 Functions

=over 4

=item new($spec, [@range])

Creates a new Set::Crontab object and returns a reference to it.

=item contains($num)

Returns true if C&lt;$num&gt; exists in the set.

=item list()

Returns the expanded list corresponding to the set. Elements are in
ascending order.

=back

The functions described above croak if they are called with incorrect
arguments.

=head1 SEE ALSO

L&lt;crontab(5)&gt;

=head1 AUTHOR

Abhijit Menon-Sen &lt;ams@toroid.org&gt;

Copyright 2001 Abhijit Menon-Sen &lt;ams@toroid.org&gt;

This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
</pre></body></html>