1 # Copyright 2001 Abhijit Menon-Sen <ams@toroid.org>
7 use vars qw( $VERSION );
13 my (@list, @and, @not);
14 my ($self, $spec, $range) = @_;
16 # 1,2-4,*/3,!13,>9,<15
17 foreach (split /,/, $spec) {
19 my $step = $1 if s#/(\d+)$##;
22 if (/^(\d+)$/) { push @pick, 0+$1; }
23 elsif (/^\*$/) { push @pick, @$range; }
24 elsif (/^(\d+)-(\d+)$/) { push @pick, 0+$1..0+$2; }
25 elsif (/^!(\d+)$/) { push @not, "\$_ != 0+$1"; }
26 elsif (/^([<>])(\d+)$/) { push @and, "\$_ $1 0+$2"; }
30 @pick = grep { defined $_ if $i++ % $step == 0 } @pick;
37 my $and = join q{ && }, @and;
38 push @list, grep { defined $_ if eval $and } @$range;
42 my $not = join q{ && }, @not;
43 @list = grep { defined $_ if eval $not } (@list ? @list : @$range);
46 @list = sort { $a <=> $b } @list;
52 my ($self, $spec, $range) = @_;
53 return undef unless ref($self);
55 croak "Usage: ".__PACKAGE__."->new(\$spec, [\@range])"
56 unless defined $spec && ref($range) eq "ARRAY";
58 $self->{LIST} = $self->_expand($spec, $range);
59 $self->{HASH} = {map {$_ => 1} @{$self->{LIST}}};
67 my $self = bless {}, ref($class) || $class;
68 return $self->_initialise(@_);
73 my ($self, $num) = @_;
75 croak "Usage: \$set->contains(\$num)" unless ref($self) && defined $num;
76 return exists $self->{HASH}{$num};
83 croak "Usage: \$set->list()" unless ref($self);
84 return @{$self->{LIST}};
92 Set::Crontab - Expand crontab(5)-style integer lists
96 $s = Set::Crontab->new("1-9/3,>15,>30,!23", [0..30]);
98 if ($s->contains(3)) { ... }
102 Set::Crontab parses crontab-style lists of integers and defines some
103 utility functions to make it easier to deal with them.
107 Numbers, ranges, *, and step values all work exactly as described in
108 L<crontab(5)>. A few extensions to the standard syntax are described
115 <N selects the elements smaller than N from the entire range, and adds
116 them to the set. >N does likewise for elements larger than N.
120 !N excludes N from the set. It applies to the other specified
121 range; otherwise it applies to the specified ranges (i.e. "!3" with a
122 range of "1-10" corresponds to "1-2,4-10", but ">3,!7" in the same range
131 =item new($spec, [@range])
133 Creates a new Set::Crontab object and returns a reference to it.
137 Returns true if C<$num> exists in the set.
141 Returns the expanded list corresponding to the set. Elements are in
146 The functions described above croak if they are called with incorrect
155 Abhijit Menon-Sen <ams@toroid.org>
157 Copyright 2001 Abhijit Menon-Sen <ams@toroid.org>
159 This module is free software; you can redistribute it and/or modify it
160 under the same terms as Perl itself.