The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Cron::Sequencer::Parser

SYNOPSIS

    my $crontab = Cron::Sequencer::Parser->new("/path/to/crontab");

DESCRIPTION

This class parses a single crontab and converts it to a form that Cron::Sequencer can use.

METHODS

new

new takes a single argument representing a crontab file to parse. Various formats are supported:

plain scalar

A file on disk

reference to a scalar

The contents of the crontab (as a single string of multiple lines)

reference to a hash
crontab

The contents of a crontab, as a single string of multiple lines. (Not a reference to a scalar containing this)

source

A file on disk. If both crontab and source are provided, then source is only used as the name of the crontab in output (and errors). No attempt is made to read the file from disk.

env

Default values for environment variables set in the crontab, as a reference to an array of strings in the form KEY=VALUE. See below for examples.

ignore

Lines in the crontab to completely ignore, as an array of integers. These are processed as the first step in the parser, so it's possible to ignore all of

  • command entries (particularly "chatty" entries such as * * * * *)

  • setting environment variables

  • lines with syntax errors that otherwise would abort the parse

This is the most flexible format. At least one of source or crontab must be specified.

The only way to provide env or ignore options is to pass a hashref.

entries

Returns a list of the crontab's command entries as data structures. Used internally by Cron::Sequencer and subject to change.

EXAMPLES

For this input

    POETS=Friday
    30 12 * * * lunch!

with default constructor options this code:

    use Cron::Sequencer;
    use Data::Dump;
    
    my $crontab = Cron::Sequencer->new({source => "reminder"});
    dd([$crontab->sequence(45000, 131400)]);

would generate this output:

    [
      [
        {
          command => "lunch!",
          env     => { POETS => "Friday" },
          file    => "reminder",
          lineno  => 2,
          time    => 45000,
          unset   => ["HUMP"],
          when    => "30 12 * * *",
        },
      ],
    ]

If we specify two environment variables:

    my $crontab = Cron::Sequencer->new({source => "reminder",
                                        env    => [
                                               "POETS=Friday",
                                               "HUMP=Wednesday"
                                           ]});

the output is:

    [
      [
        {
          command => "lunch!",
          env     => undef,
          file    => "reminder",
          lineno  => 2,
          time    => 45000,
          unset   => ["HUMP"],
          when    => "30 12 * * *",
        },
      ],
    ]

(because POETS matches the default, but HUMP was never set in the crontab)

If we ignore the first line:

    my $crontab = Cron::Sequencer->new({source => "reminder",
                                        ignore => [1]});

    [
      [
        {
          command => "lunch!",
          env     => undef,
          file    => "reminder",
          lineno  => 2,
          time    => 45000,
          unset   => undef,
          when    => "30 12 * * *",
        },
      ],
    ]

we ignore the line in the crontab that sets the environment variable.

For completeness, if we ignore the line that declares an event:

    my $crontab = Cron::Sequencer->new({source => "reminder",
                                        ignore => [2]});

there's nothing to output:

    []

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. If you would like to contribute documentation, features, bug fixes, or anything else then please raise an issue / pull request:

    https://github.com/Humanstate/cron-sequencer

AUTHOR

Nicholas Clark - nick@ccl4.org