| 1 | #!perl | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use v5.20.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | # The parts of this that we use have been stable and unchanged since v5.20.0: | ||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use feature qw(postderef); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | no warnings 'experimental::postderef'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | package Cron::Sequencer::Parser; | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 2 | require Algorithm::Cron; | |||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 2 | my %aliases = ( | |||||||||||||||||||||||||||||||
| 20 | yearly => '0 0 1 1 *', | ||||||||||||||||||||||||||||||||
| 21 | annually => '0 0 1 1 *', | ||||||||||||||||||||||||||||||||
| 22 | monthly => '0 0 1 * *', | ||||||||||||||||||||||||||||||||
| 23 | weekly => '0 0 * * 0', | ||||||||||||||||||||||||||||||||
| 24 | daily => '0 0 * * *', | ||||||||||||||||||||||||||||||||
| 25 | midnight => '0 0 * * *', | ||||||||||||||||||||||||||||||||
| 26 | hourly => '0 * * * *', | ||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # scalar -> filename | ||||||||||||||||||||||||||||||||
| 30 | # ref to scalar -> contents | ||||||||||||||||||||||||||||||||
| 31 | # hashref -> fancy | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 29 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=25 SCAR=33.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | 29 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||||||
| 35 | 29 | ▶ | confess('new() called as an instance method') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | if ref $class; | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 29 | my ($source, $crontab, $env, $ignore); | |||||||||||||||||||||||||||||||
| 39 | 29 | ▶ | if (!defined $arg) { | ||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||
| 40 | 0 | croak(__PACKAGE__ . '->new($class, $arg)'); | |||||||||||||||||||||||||||||||
| 41 | } elsif (ref $arg eq 'SCALAR') { | ||||||||||||||||||||||||||||||||
| 42 | 2 | $source = ""; | |||||||||||||||||||||||||||||||
| 43 | 2 | $crontab = $arg; | |||||||||||||||||||||||||||||||
| 44 | } elsif (ref $arg eq 'HASH') { | ||||||||||||||||||||||||||||||||
| 45 | 23 | $source = $arg->{source}; | |||||||||||||||||||||||||||||||
| 46 | $crontab = \$arg->{crontab} | ||||||||||||||||||||||||||||||||
| 47 | 23 | ▶ | if exists $arg->{crontab}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 23 | ▶ | if (exists $arg->{env}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 1 | for my $pair ($arg->{env}->@*) { | |||||||||||||||||||||||||||||||
| 50 | # vixie crontab permits empty env variable names, so we should | ||||||||||||||||||||||||||||||||
| 51 | # too we don't need it *here*, but we could implement "unset" | ||||||||||||||||||||||||||||||||
| 52 | # syntax as FOO (ie no = sign) | ||||||||||||||||||||||||||||||||
| 53 | 0 | my ($name, $value) = $pair =~ /\A([^=]*)=(.*)\z/; | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | croak("invalid environment variable assignment: '$pair'") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | unless defined $value; | ||||||||||||||||||||||||||||||||
| 56 | 0 | $env->{$name} = $value; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | 23 | ▶ | if (exists $arg->{ignore}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 18 | for my $val (map { | |||||||||||||||||||||||||||||||
| 61 | # Want to reach the croak if passed an empty string, so can't | ||||||||||||||||||||||||||||||||
| 62 | # just call split, as that returns an empty list for that input | ||||||||||||||||||||||||||||||||
| 63 | 21 | ▶ | length $_ ? split /,/, $_ : $_ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | } $arg->{ignore}->@*) { | ||||||||||||||||||||||||||||||||
| 65 | 29 | ▶ | if ($val =~ /\A([1-9][0-9]*)-([1-9][0-9]*)\z/ && $2 >= $1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$val =~ /\A([1-9][0-9]*)-([1-9][0-9]*)\z/u and $2 >= $1
MC/DC100%2/2
$val =~ /\A([1-9][0-9]*)-([1-9][0-9]*)\z/u and $2 >= $1
$val =~ /\A([1-9][0-9]*)-([1-9][0-9]*)\z/u
$2 >= $1
Truth table100%3/3
A and B
A= $val =~ /\A([1-9][0-9]*)-([1-9][0-9]*)\z/uB= $2 >= $1
| |||||||||||||||||||||||||||||||||
| 66 | ++$ignore->{$_} | ||||||||||||||||||||||||||||||||
| 67 | 8 | for $1 .. $2; | |||||||||||||||||||||||||||||||
| 68 | } elsif ($val =~ /\A[1-9][0-9]*\z/) { | ||||||||||||||||||||||||||||||||
| 69 | 14 | ++$ignore->{$val}; | |||||||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||||||
| 71 | 7 | croak("'ignore' must be a positive integer, not '$val'"); | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | } elsif (ref $arg) { | ||||||||||||||||||||||||||||||||
| 76 | 0 | confess(sprintf 'Unsupported %s reference passed to new()', ref $arg); | |||||||||||||||||||||||||||||||
| 77 | } elsif ($arg eq "") { | ||||||||||||||||||||||||||||||||
| 78 | 1 | croak("empty string is not a valid filename"); | |||||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||||||
| 80 | 3 | $source = $arg; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 21 | ▶ | if (!$crontab) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 6 | ▶ | croak("you must provide a source filename or crontab contents") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | unless length $source; | ||||||||||||||||||||||||||||||||
| 86 | 4 | ▶ | open my $fh, '<', $source | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | or croak("Can't open $source: $!"); | ||||||||||||||||||||||||||||||||
| 88 | 3 | local $/; | |||||||||||||||||||||||||||||||
| 89 | 3 | my $contents = <$fh>; | |||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | unless(defined $contents && close $fh) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $contents and close $fh
MC/DC0%0/2
defined $contents and close $fh
defined $contents
close $fh
Truth table33%1/3
A and B
A= defined $contentsB= close $fh
| |||||||||||||||||||||||||||||||||
| 91 | 0 | croak("Can't read $source: $!"); | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | 3 | $crontab = \$contents; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | # vixie crontab refuses a crontab where the last line is missing a newline | ||||||||||||||||||||||||||||||||
| 97 | # (but handles an empty file) | ||||||||||||||||||||||||||||||||
| 98 | 18 | ▶ | unless ($$crontab =~ /(?:\A|\n)\z/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | $source = length $source ? " $source" : ""; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 2 | croak("crontab$source doesn't end with newline"); | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 16 | return bless _parser($crontab, $source, $env, $ignore), $class; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 3897 | ▶ | sub _parser { | ||||||||||||||||||||||||||||||
|
Subroutine
_parser: called CC=17 SCAR=28.7
| |||||||||||||||||||||||||||||||||
| 107 | 3897 | my ($crontab, $source, $default_env, $ignore) = @_; | |||||||||||||||||||||||||||||||
| 108 | 3897 | ▶ | my $diag = length $source ? " of $source" : ""; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 3897 | my ($lineno, %env, @actions); | |||||||||||||||||||||||||||||||
| 110 | 3897 | for my $line (split "\n", $$crontab) { | |||||||||||||||||||||||||||||||
| 111 | 7828 | ++$lineno; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | next | ||||||||||||||||||||||||||||||||
| 114 | 7828 | ▶ | if $ignore->{$lineno}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # vixie crontab ignores leading tabs and spaces | ||||||||||||||||||||||||||||||||
| 117 | # See skip_comments() in misc.c | ||||||||||||||||||||||||||||||||
| 118 | # However the rest of the env parser uses isspace(), so will skip more | ||||||||||||||||||||||||||||||||
| 119 | # whitespace characters. I guess this is because the parser was | ||||||||||||||||||||||||||||||||
| 120 | # rewritten for version 4, and the more modern code can assume ANSI C. | ||||||||||||||||||||||||||||||||
| 121 | 7793 | $line =~ s/\A[ \t]+//; | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | next | ||||||||||||||||||||||||||||||||
| 124 | 7793 | ▶ | if $line =~ /\A(?:#|\z)/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | # load_env() is attempted first | ||||||||||||||||||||||||||||||||
| 127 | # Its parser has some quirks, which I have attempted to faithfully copy: | ||||||||||||||||||||||||||||||||
| 128 | 7764 | ▶ | if ($line =~ /\A | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 129 | (?: | ||||||||||||||||||||||||||||||||
| 130 | # If ' opens, a second *must* be found to close | ||||||||||||||||||||||||||||||||
| 131 | ' (*COMMIT) (?<name>[^=']*) ' | ||||||||||||||||||||||||||||||||
| 132 | | | ||||||||||||||||||||||||||||||||
| 133 | " (*COMMIT) (?<name>[^="]*) " | ||||||||||||||||||||||||||||||||
| 134 | | | ||||||||||||||||||||||||||||||||
| 135 | # The C parser accepts empty variable names | ||||||||||||||||||||||||||||||||
| 136 | (?<name>[^=\s\013]*) | ||||||||||||||||||||||||||||||||
| 137 | ) | ||||||||||||||||||||||||||||||||
| 138 | [\s\013]* = [\s\013]* | ||||||||||||||||||||||||||||||||
| 139 | (?: | ||||||||||||||||||||||||||||||||
| 140 | # If ' opens, a second *must* be found to close | ||||||||||||||||||||||||||||||||
| 141 | # *and* only trailing whitespace is permitted | ||||||||||||||||||||||||||||||||
| 142 | ' (*COMMIT) (?<value>[^']*) ' | ||||||||||||||||||||||||||||||||
| 143 | | | ||||||||||||||||||||||||||||||||
| 144 | " (*COMMIT) (?<value>[^"]*) " | ||||||||||||||||||||||||||||||||
| 145 | | | ||||||||||||||||||||||||||||||||
| 146 | # The C parser does not accept empty values | ||||||||||||||||||||||||||||||||
| 147 | (?<value>.+?) | ||||||||||||||||||||||||||||||||
| 148 | ) | ||||||||||||||||||||||||||||||||
| 149 | [\s\013]* | ||||||||||||||||||||||||||||||||
| 150 | \z | ||||||||||||||||||||||||||||||||
| 151 | /x) { | ||||||||||||||||||||||||||||||||
| 152 | 3858 | $env{$+{name}} = $+{value}; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | # else it gets passed load_entry() | ||||||||||||||||||||||||||||||||
| 155 | elsif ($line =~ /\A\@reboot[\t ]/) { | ||||||||||||||||||||||||||||||||
| 156 | # We can't handle this, as we don't know when a reboot is | ||||||||||||||||||||||||||||||||
| 157 | 1 | next; | |||||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||||||
| 159 | 3905 | my ($time, $truetime, $command); | |||||||||||||||||||||||||||||||
| 160 | 3905 | ▶ | if ($line =~ /\A\@([^\t ]+)[\t ]+(.*)\z/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 161 | 8 | $command = $2; | |||||||||||||||||||||||||||||||
| 162 | 8 | $time = '@' . $1; | |||||||||||||||||||||||||||||||
| 163 | 8 | $truetime = $aliases{$1}; | |||||||||||||||||||||||||||||||
| 164 | 8 | ▶ | croak("Unknown special string \@$1 at line $lineno$diag") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | unless $truetime; | ||||||||||||||||||||||||||||||||
| 166 | } elsif ($line =~ /\A | ||||||||||||||||||||||||||||||||
| 167 | ( | ||||||||||||||||||||||||||||||||
| 168 | [*0-9]\S* [\t ]+ | ||||||||||||||||||||||||||||||||
| 169 | [*0-9]\S* [\t ]+ | ||||||||||||||||||||||||||||||||
| 170 | [*0-9]\S* [\t ]+ | ||||||||||||||||||||||||||||||||
| 171 | \S+ [\t ]+ | ||||||||||||||||||||||||||||||||
| 172 | \S+ | ||||||||||||||||||||||||||||||||
| 173 | ) | ||||||||||||||||||||||||||||||||
| 174 | [\t ]+ | ||||||||||||||||||||||||||||||||
| 175 | ( | ||||||||||||||||||||||||||||||||
| 176 | # vixie cron explicitly forbids * here: | ||||||||||||||||||||||||||||||||
| 177 | [^*].* | ||||||||||||||||||||||||||||||||
| 178 | ) | ||||||||||||||||||||||||||||||||
| 179 | \z | ||||||||||||||||||||||||||||||||
| 180 | /x) { | ||||||||||||||||||||||||||||||||
| 181 | 3882 | $command = $2; | |||||||||||||||||||||||||||||||
| 182 | 3882 | $time = $truetime = $1; | |||||||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||||||
| 184 | 15 | croak("Can't parse '$line' at line $lineno$diag"); | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 3889 | ▶ | my $whenever = try { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 188 | 3889 | Algorithm::Cron->new( | |||||||||||||||||||||||||||||||
| 189 | base => 'utc', | ||||||||||||||||||||||||||||||||
| 190 | crontab => $truetime, | ||||||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 193 | 0 | croak("Can't parse time '$truetime' at line $lineno$diag: $_"); | |||||||||||||||||||||||||||||||
| 194 | 3889 | }; | |||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | 3889 | my %entry = ( | |||||||||||||||||||||||||||||||
| 197 | file => $source, | ||||||||||||||||||||||||||||||||
| 198 | lineno => $lineno, | ||||||||||||||||||||||||||||||||
| 199 | when => $time, | ||||||||||||||||||||||||||||||||
| 200 | command => $command, | ||||||||||||||||||||||||||||||||
| 201 | whenever => $whenever, | ||||||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 3889 | my (@unset, %set); | |||||||||||||||||||||||||||||||
| 205 | 3889 | for my $key (keys %$default_env) { | |||||||||||||||||||||||||||||||
| 206 | push @unset, $key | ||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | unless defined $env{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | 3889 | for my $key (keys %env) { | |||||||||||||||||||||||||||||||
| 210 | $set{$key} = $env{$key} | ||||||||||||||||||||||||||||||||
| 211 | 3858 | ▶ | unless defined $default_env->{$key} && $default_env->{$key} eq $env{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $default_env->{$key} and $default_env->{$key} eq $env{$key}
MC/DC0%0/2
defined $default_env->{$key} and $default_env->{$key} eq $env{$key}
defined $default_env->{$key}
$default_env->{$key} eq $env{$key}
Truth table33%1/3
A and B
A= defined $default_env->{$key}B= $default_env->{$key} eq $env{$key}
| |||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | 3889 | ▶ | $entry{unset} = [sort @unset] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 214 | if @unset; | ||||||||||||||||||||||||||||||||
| 215 | 3889 | ▶ | $entry{env} = \%set | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 216 | if %set; | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | 3889 | push @actions, \%entry; | |||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | 3881 | return \@actions; | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | # "actions", "entries", "events"? | ||||||||||||||||||||||||||||||||
| 225 | # Vixie crontab parses these with load_entry() and %ENV setting with load_env(), | ||||||||||||||||||||||||||||||||
| 226 | # so we're refer to them as entries: | ||||||||||||||||||||||||||||||||
| 227 | 15 | ▶ | sub entries { | ||||||||||||||||||||||||||||||
|
Subroutine
entries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 228 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||
| 229 | 15 | return @$self; | |||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | Cron::Sequencer::Parser | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | my $crontab = Cron::Sequencer::Parser->new("/path/to/crontab"); | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | This class parses a single crontab and converts it to a form that | ||||||||||||||||||||||||||||||||
| 243 | C<Cron::Sequencer> can use. | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | =head2 new | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | C<new> takes a single argument representing a crontab file to parse. Various | ||||||||||||||||||||||||||||||||
| 250 | formats are supported: | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | =over 4 | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | =item plain scalar | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | A file on disk | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | =item reference to a scalar | ||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | The contents of the crontab (as a single string of multiple lines) | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | =item reference to a hash | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | =over 4 | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | =item crontab | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | The contents of a crontab, as a single string of multiple lines. | ||||||||||||||||||||||||||||||||
| 269 | (Not a reference to a scalar containing this) | ||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | =item source | ||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | A file on disk. If both C<crontab> and C<source> are provided, then C<source> | ||||||||||||||||||||||||||||||||
| 274 | is only used as the name of the crontab in output (and errors). No attempt is | ||||||||||||||||||||||||||||||||
| 275 | made to read the file from disk. | ||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | =item env | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | Default values for environment variables set in the crontab, as a reference to | ||||||||||||||||||||||||||||||||
| 280 | an array of strings in the form C<KEY=VALUE>. See below for examples. | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | =item ignore | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | Lines in the crontab to completely ignore, as an array of integers. These are | ||||||||||||||||||||||||||||||||
| 285 | processed as the first step in the parser, so it's possible to ignore all of | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | =over 4 | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | =item * | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | command entries (particularly "chatty" entries such as C<* * * * *>) | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | =item * | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | setting environment variables | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | =item * | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | lines with syntax errors that otherwise would abort the parse | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | =back | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | =back | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | This is the most flexible format. At least one of C<source> or C<crontab> must | ||||||||||||||||||||||||||||||||
| 306 | be specified. | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | =back | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | The only way to provide C<env> or C<ignore> options is to pass a hashref. | ||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||
| 312 | =head2 entries | ||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||
| 314 | Returns a list of the crontab's command entries as data structures. Used | ||||||||||||||||||||||||||||||||
| 315 | internally by C<Cron::Sequencer> and subject to change. | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | =head1 EXAMPLES | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | For this input | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | POETS=Friday | ||||||||||||||||||||||||||||||||
| 322 | 30 12 * * * lunch! | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | with default constructor options this code: | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | use Cron::Sequencer; | ||||||||||||||||||||||||||||||||
| 327 | use Data::Dump; | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | my $crontab = Cron::Sequencer->new({source => "reminder"}); | ||||||||||||||||||||||||||||||||
| 330 | dd([$crontab->sequence(45000, 131400)]); | ||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | would generate this output: | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | [ | ||||||||||||||||||||||||||||||||
| 335 | [ | ||||||||||||||||||||||||||||||||
| 336 | { | ||||||||||||||||||||||||||||||||
| 337 | command => "lunch!", | ||||||||||||||||||||||||||||||||
| 338 | env => { POETS => "Friday" }, | ||||||||||||||||||||||||||||||||
| 339 | file => "reminder", | ||||||||||||||||||||||||||||||||
| 340 | lineno => 2, | ||||||||||||||||||||||||||||||||
| 341 | time => 45000, | ||||||||||||||||||||||||||||||||
| 342 | unset => ["HUMP"], | ||||||||||||||||||||||||||||||||
| 343 | when => "30 12 * * *", | ||||||||||||||||||||||||||||||||
| 344 | }, | ||||||||||||||||||||||||||||||||
| 345 | ], | ||||||||||||||||||||||||||||||||
| 346 | ] | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | If we specify two environment variables: | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | my $crontab = Cron::Sequencer->new({source => "reminder", | ||||||||||||||||||||||||||||||||
| 351 | env => [ | ||||||||||||||||||||||||||||||||
| 352 | "POETS=Friday", | ||||||||||||||||||||||||||||||||
| 353 | "HUMP=Wednesday" | ||||||||||||||||||||||||||||||||
| 354 | ]}); | ||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | the output is: | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | [ | ||||||||||||||||||||||||||||||||
| 359 | [ | ||||||||||||||||||||||||||||||||
| 360 | { | ||||||||||||||||||||||||||||||||
| 361 | command => "lunch!", | ||||||||||||||||||||||||||||||||
| 362 | env => undef, | ||||||||||||||||||||||||||||||||
| 363 | file => "reminder", | ||||||||||||||||||||||||||||||||
| 364 | lineno => 2, | ||||||||||||||||||||||||||||||||
| 365 | time => 45000, | ||||||||||||||||||||||||||||||||
| 366 | unset => ["HUMP"], | ||||||||||||||||||||||||||||||||
| 367 | when => "30 12 * * *", | ||||||||||||||||||||||||||||||||
| 368 | }, | ||||||||||||||||||||||||||||||||
| 369 | ], | ||||||||||||||||||||||||||||||||
| 370 | ] | ||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||
| 372 | (because C<POETS> matches the default, but C<HUMP> was never set in the crontab) | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | If we ignore the first line: | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | my $crontab = Cron::Sequencer->new({source => "reminder", | ||||||||||||||||||||||||||||||||
| 377 | ignore => [1]}); | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | [ | ||||||||||||||||||||||||||||||||
| 380 | [ | ||||||||||||||||||||||||||||||||
| 381 | { | ||||||||||||||||||||||||||||||||
| 382 | command => "lunch!", | ||||||||||||||||||||||||||||||||
| 383 | env => undef, | ||||||||||||||||||||||||||||||||
| 384 | file => "reminder", | ||||||||||||||||||||||||||||||||
| 385 | lineno => 2, | ||||||||||||||||||||||||||||||||
| 386 | time => 45000, | ||||||||||||||||||||||||||||||||
| 387 | unset => undef, | ||||||||||||||||||||||||||||||||
| 388 | when => "30 12 * * *", | ||||||||||||||||||||||||||||||||
| 389 | }, | ||||||||||||||||||||||||||||||||
| 390 | ], | ||||||||||||||||||||||||||||||||
| 391 | ] | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | we ignore the line in the crontab that sets the environment variable. | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | For completeness, if we ignore the line that declares an event: | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | my $crontab = Cron::Sequencer->new({source => "reminder", | ||||||||||||||||||||||||||||||||
| 398 | ignore => [2]}); | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | there's nothing to output: | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | [] | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 408 | the same terms as Perl itself. If you would like to contribute documentation, | ||||||||||||||||||||||||||||||||
| 409 | features, bug fixes, or anything else then please raise an issue / pull request: | ||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | https://github.com/Humanstate/cron-sequencer | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | Nicholas Clark - C<nick@ccl4.org> | ||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | =cut | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | 2 | 1; | |||||||||||||||||||||||||||||||