.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Crontab 3pm" .TH Crontab 3pm "2022-10-22" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Config::Crontab \- Read/Write Vixie compatible crontab(5) files .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Config::Crontab; \& \& #################################### \& ## making a new crontab from scratch \& #################################### \& \& my $ct = new Config::Crontab; \& \& ## make a new Block object \& my $block = new Config::Crontab::Block( \-data => <<_BLOCK_ ); \& ## mail something to joe at 5 after midnight on Fridays \& MAILTO=joe \& 5 0 * * Fri /bin/someprogram 2>&1 \& _BLOCK_ \& \& ## add this block to the crontab object \& $ct\->last($block); \& \& ## make another block using Block methods \& $block = new Config::Crontab::Block; \& $block\->last( new Config::Crontab::Comment( \-data => \*(Aq## do backups\*(Aq ) ); \& $block\->last( new Config::Crontab::Env( \-name => \*(AqMAILTO\*(Aq, \-value => \*(Aqbob\*(Aq ) ); \& $block\->last( new Config::Crontab::Event( \-minute => 40, \& \-hour => 3, \& \-command => \*(Aq/sbin/backup \-\-partition=all\*(Aq ) ); \& ## add this block to crontab file \& $ct\->last($block); \& \& ## write out crontab file \& $ct\->write; \& \& ############################### \& ## changing an existing crontab \& ############################### \& \& my $ct = new Config::Crontab; $ct\->read; \& \& ## comment out the command that runs our backup \& $_\->active(0) for $ct\->select(\-command_re => \*(Aq/sbin/backup\*(Aq); \& \& ## save our crontab again \& $ct\->write; \& \& ############################### \& ## read joe\*(Aqs crontab (must have root permissions) \& ############################### \& \& ## same as "crontab \-u joe \-l" \& my $ct = new Config::Crontab( \-owner => \*(Aqjoe\*(Aq ); \& $ct\->read; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fBConfig::Crontab\fR provides an object-oriented interface to Vixie-style \fBcrontab\fR\|(5) files for Perl. .PP A \fBConfig::Crontab\fR object allows you to manipulate an ordered set of \fBEvent\fR, \fBEnv\fR, or \fBComment\fR objects (also included with this package). Descriptions of these packages may be found below. .PP In short, \fBConfig::Crontab\fR reads and writes \fBcrontab\fR\|(5) files (and does a little pretty-printing too) using objects. The general idea is that you create a \fBConfig::Crontab\fR object and associate it with a file (if unassociated, it will work over a pipe to \f(CW\*(C`crontab \-l\*(C'\fR). From there, you can add lines to your crontab object, change existing line attributes, and write everything back to file. .IP "\(bu" 4 \&\s-1NOTE:\s0 \fBConfig::Crontab\fR does \fInot\fR (currently) do validity checks on your data (i.e., dates out of range, etc.). However, if the call to \fBcrontab\fR fails when you invoke \fBwrite\fR, \fBwrite\fR will return \&\fIundef\fR and set \fBerror\fR with the error message returned from the \&\fBcrontab\fR command. Future development may tend toward more validity checks. .PP Now, to successfully navigate the module's ins and outs, we'll need a little terminology lesson. .SS "Terminology" .IX Subsection "Terminology" \&\fBConfig::Crontab\fR (hereafter simply \fBCrontab\fR) sees a \f(CW\*(C`crontab\*(C'\fR file in terms of \fIblocks\fR. A block is simply an ordered set of one or more lines. Blocks are separated by two or more newlines. For example, here is a crontab file with two blocks: .PP .Vb 2 \& ## a comment \& 30 4 * * * /bin/some_command \& \& ## another comment \& ENV=some_value \& 50 9 * * 1\-5 /bin/reminder \-\-meeting=friday .Ve .PP The first block contains two \fBConfig::Crontab::*\fR objects: a \&\fBComment\fR object and an \fBEvent\fR object. The second block contains an \fBEnv\fR object in addition to a \fBComment\fR object and an \fBEvent\fR object. The \fBConfig::Crontab\fR class, then, consists of zero or more \&\fBConfig::Crontab::Block\fR objects. \fBBlock\fR objects have these three basic elements: .IP "\fBConfig::Crontab::Event\fR" 4 .IX Item "Config::Crontab::Event" Any lines in a crontab that look like these are \fBEvent\fR objects: .Sp .Vb 3 \& 5 10 * * * /some/command \& @reboot /bin/mystartup.sh \& ## 0 0 * * Fri /disabled/command .Ve .Sp Notice that commented out event lines are still considered \fBEvent\fR objects. .Sp \&\fBEvent\fR objects are described below in the \fBEvent\fR package description. Please refer to it for details on manipulating \fBEvent\fR objects. .IP "\fBConfig::Crontab::Env\fR" 4 .IX Item "Config::Crontab::Env" Any lines in a crontab that look like these are \fBEnv\fR objects: .Sp .Vb 3 \& MAILTO=joe \& SOMEVAR = some_value \& #DISABLED=env_setting .Ve .Sp Notice that commented out environment lines are still considered \&\fBEnv\fR objects. .Sp \&\fBEnv\fR objects are described below in the \fBEnv\fR package description. Please refer to it for details on manipulating \fBEnv\fR objects. .IP "\fBConfig::Crontab::Comment\fR" 4 .IX Item "Config::Crontab::Comment" Any lines containing only whitespace or lines beginning with a pound sign (but are not \fBEvent\fR or \fBEnv\fR objects) are \fBComment\fR objects: .Sp .Vb 2 \& ## this is a comment \& (imagine somewhitespace here) .Ve .Sp \&\fBComment\fR objects are described below in the \fBComment\fR package description. Please refer to it for details on manipulating \fBComment\fR objects. .SS "Illustration" .IX Subsection "Illustration" Here is a simple crontab file: .PP .Vb 1 \& MAILTO=joe@schmoe.org \& \& ## send reminder in April \& 3 10 * Apr Fri joe echo "Friday a.m. in April" .Ve .PP The file consists of an environment variable setting (\s-1MAILTO\s0), a comment, and a command to run. After parsing the above file, \&\fBConfig::Crontab\fR would break it up into the following objects: .PP .Vb 10 \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | Config::Crontab object | \& | | \& | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | \& | | Config::Crontab::Block object | | \& | | | | \& | | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | | \& | | | Config::Crontab::Env object | | | \& | | | | | | \& | | | \-name => MAILTO | | | \& | | | \-value => joe@schmoe.org | | | \& | | | \-data => MAILTO=joe@schmoe.org | | | \& | | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | | \& | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | \& | | \& | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | \& | | Config::Crontab::Block object | | \& | | | | \& | | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | | \& | | | Config::Crontab::Comment object | | | \& | | | | | | \& | | | \-data => ## send reminder in April | | | \& | | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | | \& | | | | \& | | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | | \& | | | Config::Crontab::Event Object | | | \& | | | | | | \& | | | \-datetime => 3 10 * Apr Fri | | | \& | | | \-special => (empty) | | | \& | | | \-minute => 3 | | | \& | | | \-hour => 10 | | | \& | | | \-dom => * | | | \& | | | \-month => Apr | | | \& | | | \-dow => Fri | | | \& | | | \-user => joe | | | \& | | | \-command => echo "Friday a.m. in April" | | | \& | | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | | \& | +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ .Ve .PP You'll notice the main Config::Crontab object encapsulates the entire file. The parser found two \fBBlock\fR objects: the lone \s-1MAILTO\s0 variable setting, and the comment and command (together). Two or more newlines together in a crontab file constitute a block separator. This allows you to logically group commands (as most people do anyway) in the crontab file, and work with them as a Config::Crontab::Block objects. .PP The second block consists of a \fBComment\fR object and an \fBEvent\fR object, shown are some of the data methods you can use to get or set data in those objects. .SS "Practical Usage: A Brief Tutorial" .IX Subsection "Practical Usage: A Brief Tutorial" Now that we know what \fBConfig::Crontab\fR objects look like and what they're called, let's play around a little. .PP Let's say we have an existing crontab on many machines that we want to manage. The crontab contains some machine-dependent information (e.g., timezone, etc.), so we can't just copy a file out everywhere and replace the existing crontab. We need to edit each crontab individually, specifically, we need to change the time when a particular job runs: .PP .Vb 1 \& 30 2 * * * /usr/local/sbin/pirate \-\-arg=matey .Ve .PP to 3:30 am because of daylight saving time (i.e., we don't want this job to run twice). .PP We can do something like this: .PP .Vb 1 \& use Config::Crontab; \& \& my $ct = new Config::Crontab; \& $ct\->read; \& \& my ($event) = $ct\->select(\-command_re => \*(Aqpirate \-\-arg=matey\*(Aq); \& $event\->hour(3); \& \& $ct\->write; .Ve .PP All done! This shows us a couple of subtle but important points: .IP "\(bu" 4 The \fBConfig::Crontab\fR object must have its \fBread\fR method invoked for it to read the crontab file. .IP "\(bu" 4 The \fBselect\fR method returns a list, even if there is only one item to return. This is why we put parentheses around \fI\f(CI$event\fI\fR (otherwise we would be putting the return value of \fBselect\fR into scalar context and we would get the number of items in the list instead of the list itself). .IP "\(bu" 4 The \fIset\fR methods for \fBEvent\fR (and other) objects are usually invoked the same way as their \fIget\fR method except with an argument. .IP "\(bu" 4 We must write the crontab back out to file with the \fBwrite\fR method. .PP Here's how we might do the same thing in a one-line Perl program: .PP .Vb 3 \& perl \-MConfig::Crontab \-e \*(Aq$ct=new Config::Crontab; $ct\->read; \e \& ($ct\->select(\-command_re=>"pirate \-\-arg=matey"))[0]\->hour(3); \e \& $ct\->write\*(Aq .Ve .PP Nice! Ok. Now we need to add a new crontab entry: .PP .Vb 1 \& 35 6 * * * /bin/alarmclock \-\-ring .Ve .PP We can do it like this: .PP .Vb 6 \& $event = new Config::Crontab::Event( \-minute => 36, \& \-hour => 6, \& \-command => \*(Aq/bin/alarmclock \-\-ring\*(Aq); \& $block = new Config::Crontab::Block; \& $block\->last($event); \& $ct\->last($block); .Ve .PP or like this: .PP .Vb 2 \& $event = new Config::Crontab::Event( \-data => \*(Aq35 6 * * * /bin/alarmclock \-\-ring\*(Aq ); \& $ct\->last(new Config::Crontab::Block( \-lines => [$event] )); .Ve .PP or like this: .PP .Vb 1 \& $ct\->last(new Config::Crontab::Block(\-data => "35 6 * * * /bin/alarmclock \-\-ring")); .Ve .PP We learn the following things from this example: .IP "\(bu" 4 Only \fBBlock\fR objects can be added to \fBCrontab\fR objects (see \&\*(L"\s-1CAVEATS\*(R"\s0). \fBBlock\fR objects may be added via the \fBlast\fR method (and several other methods, including \fBfirst\fR, \fBup\fR, \fBdown\fR, \&\fBbefore\fR, and \fBafter\fR). .IP "\(bu" 4 \&\fBBlock\fR objects can be populated in a variety of ways, including the \&\fB\-data\fR attribute (a string which may\*(--and frequently does\*(--span multiple lines via a 'here' document), the \fB\-lines\fR attribute (which takes a list reference), and the \fBlast\fR method. In addition to the \&\fBlast\fR method, \fBBlock\fR objects use the same methods for adding and moving objects that the \fBCrontab\fR object does: \fBfirst\fR, \fBlast\fR, \&\fBup\fR, \fBdown\fR, \fBbefore\fR, and \fBafter\fR. .PP After the \fBModule Utility\fR section, the remainder of this document is a reference manual and describes the methods available (and how to use them) in each of the 5 classes: \fBConfig::Crontab\fR, \&\fBConfig::Crontab::Block\fR, \fBConfig::Crontab::Event\fR, \&\fBConfig::Crontab::Env\fR, and \fBConfig::Crontab::Comment\fR. The reader is also encouraged to look at the example \s-1CGI\s0 script in the \fIeg\fR directory and the (somewhat contrived) examples in the \fIt\fR (testing) directory with this distribution. .SS "Module Utility" .IX Subsection "Module Utility" \&\fBConfig::Crontab\fR is a useful module by virtue of the \*(L"one-liner\*(R" test. A useful module must do useful work (editing crontabs is useful work) economically (i.e., useful work must be able to be done on a single command-line that doesn't wrap more than twice and can be understood by an adept Perl programmer). .PP Graham Barr's \fBNet::POP3\fR module (actually, most of Graham's work falls in this category) is a good example of a useful module. .PP So, with no more ado, here are some useful one-liners with \&\fBConfig::Crontab\fR: .IP "\(bu" 4 uncomment all crontab events whose command contains the string 'fetchmail' .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $_\->active(1) for $c\->select(\-command_re => "fetchmail"); $c\->write\*(Aq .Ve .IP "\(bu" 4 remove the first crontab block that has '/bin/unwanted' as a command .Sp .Vb 3 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $c\->remove($c\->block($c\->select(\-command_re => "/bin/unwanted"))); \e \& $c\->write\*(Aq .Ve .IP "\(bu" 4 reschedule the backups to run just Monday thru Friday: .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $_\->dow("1\-5") for $c\->select(\-command_re => "/sbin/backup"); $c\->write\*(Aq .Ve .IP "\(bu" 4 reschedule the backups to run weekends too: .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $_\->dow("*") for $c\->select(\-command_re => "/sbin/backup"); $c\->write\*(Aq .Ve .IP "\(bu" 4 change all '\s-1MAILTO\s0' environment settings in this crontab to 'joe@schmoe.org': .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $_\->value(q!joe@schmoe.org!) for $c\->select(\-name => "MAILTO"); $c\->write\*(Aq .Ve .IP "\(bu" 4 strip all comments from a crontab: .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $c\->remove($c\->select(\-type => "comment")); $c\->write\*(Aq .Ve .IP "\(bu" 4 disable an entire block of commands (the block that has the word \&'Friday' in it): .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c=new Config::Crontab; $c\->read; \e \& $c\->block($c\->select(\-data_re => "Friday"))\->active(0); $c\->write\*(Aq .Ve .IP "\(bu" 4 copy one user's crontab to another user: .Sp .Vb 2 \& perl \-MConfig::Crontab \-e \*(Aq$c = new Config::Crontab(\-owner => "joe"); \e \& $c\->read; $c\->owner("mike"); $c\->write\*(Aq .Ve .SH "PACKAGE Config::Crontab" .IX Header "PACKAGE Config::Crontab" This section describes \fBConfig::Crontab\fR objects (hereafter simply \&\fBCrontab\fR objects). A \fBCrontab\fR object is an abstracted way of dealing with an entire \fB\fBcrontab\fB\|(5)\fR file. The \fBCrontab\fR class has methods to allow you to select, add, or remove \fBBlock\fR objects as well as read and parse crontab files and write crontab files. .SS "init([%args])" .IX Subsection "init([%args])" This method is called implicitly when you instantiate an object via \&\fBnew\fR. \fBinit\fR takes the same arguments as \fBnew\fR and \fBread\fR. If the \fB\-file\fR argument is specified (and is non-false), \fBinit\fR will invoke \fBread\fR automatically with the \fB\-file\fR value. Use \fBinit\fR to re-initialize an object. .PP Example: .PP .Vb 2 \& ## auto\-parses foo.txt in implicit call to init \& $ct = new Config::Crontab( \-file => \*(Aqfoo.txt\*(Aq ); \& \& ## re\-initialize the object with default values and a new file \& $ct\->init( \-file => \*(Aqbar.txt\*(Aq ); .Ve .SS "strict([boolean])" .IX Subsection "strict([boolean])" \&\fBstrict\fR enforces the following constraints: .IP "\(bu" 4 if the file specified by the \fBfile\fR method (or \fB\-file\fR attribute in \&\fBnew\fR) does not exist at the time \fBread\fR is invoked, \fBread\fR sets \&\fBerror\fR and dies: \*(L"Could not open (filename): (reason)\*(R". If strict is disabled, \fBread\fR returns \fIundef\fR (\fBerror\fR is set). .IP "\(bu" 4 If the file specified by the \fBfile\fR method (or \fB\-file\fR attribute in \&\fBnew\fR) cannot be written to, or the \f(CW\*(C`crontab\*(C'\fR command fails, \&\fBwrite\fR sets \fBerror\fR and warns: \*(L"Could not open (filename): (reason)\*(R". If strict is disabled, \fBwrite\fR returns \fIundef\fR (\fBerror\fR is set). .IP "\(bu" 4 Croaks if an illegal username is specified in the \fB\-owner\fR parameter. .PP Examples: .PP .Vb 2 \& ## disable strict (default) \& $ct\->strict(0); .Ve .SS "system([boolean])" .IX Subsection "system([boolean])" \&\fBsystem\fR tells \fBconfig::crontab\fR to assume that the crontab object is after the pattern described in \fBcrontab\fR\|(5) with an extra \fIuser\fR field before the \fIcommand\fR field: .PP .Vb 1 \& @reboot joeuser /usr/local/bin/fetchmail \-d 300 .Ve .PP where the given command will be executed by said user. when a crontab file (e.g., \fI/etc/crontab\fR) is parsed without \fBsystem\fR enabled, the \&\fIuser\fR field will be lumped in with the command. When enabled, the user field will be accessible in each event object via the \fBuser\fR method (see \*(L"user\*(R" in the \fBevent\fR documentation below). .SS "owner([string])" .IX Subsection "owner([string])" \&\fBowner\fR sets the owner of the crontab. If you're running Config::Crontab as a privileged user (e.g., \*(L"root\*(R"), you can read and write user crontabs by specifying \fBowner\fR either in the constructor, during \fBinit\fR, or using \fBowner\fR before a \fBread\fR or \fBwrite\fR method is called: .PP .Vb 2 \& $c = new Config::Crontab( \-owner => \*(Aqjoe\*(Aq ); \& $c\->read; ## reading joe\*(Aqs crontab .Ve .PP Or another way: .PP .Vb 3 \& $c = new Config::Crontab; \& $c\->owner(\*(Aqjoe\*(Aq); \& $c\->read; ## reading joe\*(Aqs crontab .Ve .PP You can use this to copy a crontab from one user to another: .PP .Vb 4 \& $c\->owner(\*(Aqjoe\*(Aq); \& $c\->read; \& $c\->owner(\*(Aqbob\*(Aq); \& $c\->write; .Ve .SS "owner_re([regex])" .IX Subsection "owner_re([regex])" \&\fBConfig::Crontab\fR is strict in what it will allow for a username, since this information internally is passed to a shell. If the username specified is not a user on the system, \fBConfig::Crontab\fR will set \fBerror\fR with \*(L"Illegal username\*(R" and return \fIundef\fR; if \&\fBstrict\fR mode is enabled, \fBConfig::Crontab\fR will croak with the same error. .PP Further, once the username is determined valid, the username is then checked against a regular expression to thwart null string attacks and other maliciousness. The default regular expression used to check for a safe username is: .PP .Vb 1 \& /[^a\-zA\-Z0\-9\e._\-]/ .Ve .PP If the pattern matches (i.e., if any characters other than the ones above are found in the supplied username), \fBConfig::Crontab\fR will set \fBerror\fR with \*(L"Illegal username\*(R" and return \fIundef\fR. If \fBstrict\fR mode is enabled, \fBConfig::Crontab\fR will croak with the same error. .PP .Vb 1 \& $c\->owner_re(\*(Aq[^a\-zA\-Z0\-9_\e.\-#]\*(Aq); ## allow # in usernames .Ve .SS "read([%args])" .IX Subsection "read([%args])" Parses the crontab file specified by \fBfile\fR. If \fBfile\fR is not set (or is false in some way), the crontab will be read from a pipe to \&\f(CW\*(C`crontab \-l\*(C'\fR. \fBread\fR optionally takes the same arguments as \fBnew\fR and \fBinit\fR in \f(CW\*(C`key => value\*(C'\fR style lists. .PP Until you \fBread\fR the crontab, the \fBCrontab\fR object will be uninitialized and will contain no data. You may re-read existing objects to get new crontab data, but the object will retain whatever other attributes (e.g., strict, etc.) it may have from when it was initialized (or later attributes were changed) but will reset \&\fBerror\fR. Use \fBinit\fR to completely refresh an object. .PP If \fBread\fR fails, \fBerror\fR will be set. .PP Examples: .PP .Vb 3 \& ## reads the crontab for this UID (via crontab \-l) \& $ct = new Config::Crontab; \& $ct\->read; \& \& ## reads the crontab from a file \& $ct = new Config::Crontab; \& $ct\->read( \-file => \*(Aq/var/cronbackups/cron1\*(Aq ); \& \& ## same thing as above \& $ct = new Config::Crontab( \-file => \*(Aq/var/cronbackups/cron1\*(Aq ); \& $ct\->read; ## \*(Aq\-file\*(Aq attribute already set \& \& ## ditto using \*(Aqfile\*(Aq method \& $ct = new Config::Crontab; \& $ct\->file(\*(Aq/var/cronbackups/cron1\*(Aq); \& $ct\->read; \& \& ## ditto, using a pipe \& $ct = new Config::Crontab; \& $ct\->file(\*(Aqcat /var/cronbackups/cron1|\*(Aq); \& $ct\->read; \& \& ## ditto, using \*(Aqread\*(Aq method \& $ct = new Config::Crontab; \& $ct\->read( \-file => \*(Aqcat /var/cronbackups/cron1|\*(Aq); \& \& ## now fortified with error\-checking \& $ct\->read \& or do { \& warn $ct\->error; \& return; \& }; .Ve .SS "mode([mode])" .IX Subsection "mode([mode])" Returns the current parsing mode for this object instance. If a mode is passed as an argument, next time this instance parses a crontab file, it will use this new mode. Valid modes are \fIline\fR, \fIblock\fR (the default), or \fIfile\fR. .PP Example: .PP .Vb 3 \& ## re\-read this crontab in \*(Aqfile\*(Aq mode \& $ct\->mode(\*(Aqfile\*(Aq); \& $ct\->read; .Ve .SS "blocks([\e@blocks])" .IX Subsection "blocks([@blocks])" Returns a list of \fBBlock\fR objects in this crontab. The \fBblocks\fR method also takes an optional list reference as an argument to set this crontab's block list. .PP Example: .PP .Vb 7 \& ## get blocks, remove comments and dump \& for my $block ( $ct\->blocks ) { \& $block\->remove($block\->select( \-type => \*(Aqcomment\*(Aq ) ); \& $block\->remove($block\->select( \-type => \*(Aqevent\*(Aq, \& \-active => 0 ); \& print $block\->dump; \& } \& \& ## one way to remove unwanted blocks from a crontab \& my @keepers = $ct\->select( \-type => \*(Aqcomment\*(Aq, \& \-data_re => \*(Aqkeep this block\*(Aq ); \& $ct\->blocks(\e@keepers); \& \& ## another way to do it (notice \*(Aqnre\*(Aq instead of \*(Aqre\*(Aq) \& $ct\->remove($ct\->select( \-type => \*(Aqcomment\*(Aq, \& \-data_nre => \*(Aqkeep this block\*(Aq )); .Ve .SS "select([%criteria])" .IX Subsection "select([%criteria])" Returns a list of crontab lines that match the specified criteria. Multiple criteria may be specified. If no criteria are specified, \&\fBselect\fR returns a list of all lines in the \fBCrontab\fR object. .PP Field names should be preceded by a hyphen (though without a hyphen is acceptable too). .PP The following criteria and associated values are available: .IP "\(bu" 4 \&\-type .Sp One of 'event', 'env', or 'comment' .IP "\(bu" 4 \&\- .Sp The object in the block will be matched using 'eq' (string comparison) against this criterion. .IP "\(bu" 4 \&\-_re .Sp The value of the object method specified will be matched using Perl regular expressions (see perlre) instead of string comparisons (uses the \f(CW\*(C`=~\*(C'\fR operator internally). .IP "\(bu" 4 \&\-_nre .Sp The value of the object method specified will be negatively matched using Perl regular expressions (see perlre) instead of string comparisons (uses the \f(CW\*(C`!~\*(C'\fR operator internally). .PP Examples: .PP .Vb 4 \& ## returns a list of comments in the crontab that matches the \& ## exact phrase \*(Aq## I like bread\*(Aq \& @comments = $ct\->select( \-type => \*(Aqcomment\*(Aq, \& \-data => \*(Aq## I like bread\*(Aq ); \& \& ## returns a list of comments in the crontab that match the \& ## regular expression \*(AqI like bread\*(Aq \& @comments = $ct\->select( \-type => \*(Aqcomment\*(Aq, \& \-data_re => \*(AqI like bread\*(Aq ); \& \& ## select all cron jobs likely to repeat during daylight saving \& @events = $ct\->select( \-type => \*(Aqevent\*(Aq, \& \-hour => \*(Aq2\*(Aq ); \& \& ## select cron jobs that happen from 10:20 to 10:40 on Fridays \& @events = $ct\->select( \-type => \*(Aqevent\*(Aq, \& \-hour => \*(Aq10\*(Aq, \& \-minute_re => \*(Aq^(?:[2\-3][0\-9]|40)$\*(Aq, \& \-dow_re => \*(Aq(?:5|Fri)\*(Aq ); \& \& ## select all cron jobs that execute during business hours \& @events = $ct\->select( \-type => \*(Aqevent\*(Aq, \& \-hour_re => \*(Aq^(?:[8\-9]|1[0\-6])$\*(Aq ); \& \& ## select all cron jobs that don\*(Aqt execute during business hours \& @events = $ct\->select( \-type => \*(Aqevent\*(Aq, \& \-hour_nre => \*(Aq^(?:[8\-9]|1[0\-6])$\*(Aq ); \& \& ## get all event lines in the crontab \& @events = $ct\->select( \-type => \*(Aqevent\*(Aq ); \& \& ## get all lines in the crontab \& @lines => $ct\->select; \& \& ## get a line: note list context, also, no \*(Aqtype\*(Aq specified \& ($line) = $ct\->select( \-data_re => \*(Aqstart backups\*(Aq ); .Ve .SS "select_blocks([%criteria])" .IX Subsection "select_blocks([%criteria])" Returns a list of crontab Block objects that match the specified criteria. If no criteria are specified, \fBselect_blocks\fR behaves just like the \fBblocks\fR method, returning all blocks in the crontab object. .PP The following criteria keys are available: .IP "\(bu" 4 \&\-index .Sp An integer or list reference of integers. Returns a list of blocks indexed by the given integer(s). .Sp Example: .Sp .Vb 2 \& ## select the first block in the file \& @blocks = $ct\->select_blocks( \-index => 1 ); \& \& ## select blocks 1, 5, 6, and 7 \& @blocks = $ct\->select_blocks( \-index => [1, 5, 6, 7] ); .Ve .PP \&\fBselect_blocks\fR returns \fBBlock\fR objects, which means that if you need to access data elements inside the blocks, you'll need to retrieve them using \fBlines\fR or \fBselect\fR method first: .PP .Vb 4 \& ## the first block in the crontab file is an environment variable \& ## declaration: NAME=value \& @blocks = $ct\->select_blocks( \-index => 1 ); \& print "This environment variable value is " . ($block[0]\->lines)[0]\->value . "\en"; .Ve .SS "block($line)" .IX Subsection "block($line)" Returns the block that this line belongs to. If the line is not found in any blocks, \fIundef\fR is returned. \fI\f(CI$line\fI\fR must be a \&\fBConfig::Crontab::Event\fR, \fBConfig::Crontab::Env\fR, or \&\fBConfig::Crontab::Comment\fR object. .PP Examples: .PP .Vb 2 \& ## will always return undef for new objects; you\*(Aqd never really do this \& $block = $ct\->block( new Config::Crontab::Comment(\-data => \*(Aq## foo\*(Aq) ); \& \& ## returns a Block object \& $block = $ct\->block($existing_crontab_line); \& $block\->dump; \& \& ## find and remove the block in which \*(Aq/bin/baz\*(Aq is executed \& my $event = $ct\->select( \-type => \*(Aqevent\*(Aq, \& \-command_re => \*(Aq/bin/baz\*(Aq); \& $block = $ct\->block($event); \& $ct\->remove($block); .Ve .SS "remove($block)" .IX Subsection "remove($block)" Removes a block from the crontab file (if a block is specified) or a crontab line from its block (if a crontab line object is specified). .PP Example: .PP .Vb 2 \& ## remove this block from the crontab \& $ct\->remove($block); \& \& ## remove just a line from its block \& $ct\->remove($line); .Ve .ie n .SS "replace($oldblock, $newblock)" .el .SS "replace($oldblock, \f(CW$newblock\fP)" .IX Subsection "replace($oldblock, $newblock)" Replaces \fI\f(CI$oldblock\fI\fR with \fI\f(CI$newblock\fI\fR. Returns \fI\f(CI$oldblock\fI\fR if successful, \fIundef\fR otherwise. .PP Example: .PP .Vb 4 \& ## look for the block containing \*(Aqoldtuesday\*(Aq and replace it with our new block \& $newblock = new Config::Crontab::Block( \-data => \*(Aq5 10 * * Tue /bin/tuesday\*(Aq ); \& my $oldblock = $ct\->block($ct\->select(\-data_re => \*(Aqoldtuesday\*(Aq)); \& $ct\->replace($oldblock, $newblock); .Ve .SS "up($block), down($block)" .IX Subsection "up($block), down($block)" These methods move a single \fBConfig::Crontab::Block\fR object up or down in the \fBCrontab\fR object's internal array. If the \fBBlock\fR object is not already a member of this array, it will be added to the array in the first position (for \fBup\fR) and in the last position (for \&\fBdown\fR. See also \fBfirst\fR and \fBlast\fR and \fBup\fR and \fBdown\fR in the \&\fBBlock\fR class. .PP Example: .PP .Vb 1 \& $ct\->up($block); ## move this block up one position .Ve .SS "first(@block), last(@block)" .IX Subsection "first(@block), last(@block)" These methods move the \fBConfig::Crontab::Block\fR object(s) to the first or last positions in the \fBCrontab\fR object's internal array. If the block is not already a member of the array, it will be added in the first or last position respectively. .PP Example: .PP .Vb 4 \& $ct\->last(new Config::Crontab::Block( \-data => <<_BLOCK_ )); \& ## eat ice cream \& 5 * * * 1\-5 /bin/eat \-\-cream=ice \& _BLOCK_ .Ve .ie n .SS "before($look_for, @blocks), after($look_for, @blocks)" .el .SS "before($look_for, \f(CW@blocks\fP), after($look_for, \f(CW@blocks\fP)" .IX Subsection "before($look_for, @blocks), after($look_for, @blocks)" These methods move the \fBConfig::Crontab::Block\fR object(s) to the position immediately before or after the \fI\f(CI$look_for\fI\fR (or reference) block in the \fBCrontab\fR object's internal array. .PP If the objects are not members of the array, they will be added before or after the reference block respectively. If the reference object does not exist in the array, the blocks will be moved (or added) to the beginning or end of the array respectively (like \fBfirst\fR and \&\fBlast\fR). .PP Example: .PP .Vb 2 \& ## search for a block containing a particular event (line) \& $block = $ct\->block($ct\->select(\-command_re => \*(Aq/bin/foo\*(Aq)); \& \& ## add the new blocks immediately after this block \& $ct\->after($block, @new_blocks); .Ve .SS "write([$filename])" .IX Subsection "write([$filename])" Writes the crontab to the file specified by the \fBfile\fR method. If \&\fBfile\fR is not set (or is false), \fBwrite\fR will attempt to write to a temporary file and load it via the \f(CW\*(C`crontab\*(C'\fR program (e.g., \&\f(CW\*(C`crontab filename\*(C'\fR). .PP You may specify an optional filename as an argument to set \fBfile\fR, which will then be used as the filename. .PP If \fBwrite\fR fails, \fBerror\fR will be set. .PP Example: .PP .Vb 6 \& ## write out crontab \& $ct\->write \& or do { \& warn "Error: " . $ct\->error . "\en"; \& return; \& }; \& \& ## set \*(Aqfile\*(Aq and write simultaneously (future calls to read and \& ## write will use this filename) \& $ct\->write(\*(Aq/var/mycronbackups/cron1.txt\*(Aq); \& \& ## same thing \& $ct\->file(\*(Aq/var/mycronbackups/cron1.txt\*(Aq); \& $ct\->write; .Ve .SS "remove_tab([file])" .IX Subsection "remove_tab([file])" Removes a crontab. If \fBfile\fR is set, that file will be unlinked. If \&\fBfile\fR is not set (or is false), \fBremove_tab\fR will attempt to remove the selected user's crontab via \fIcrontab \-u username \-r\fR or \fIcrontab \&\-r\fR for the current user id. .PP If \fBremove_tab\fR fails, \fBerror\fR will be set. .PP Example: .PP .Vb 1 \& $ct\->remove_tab(\*(Aq\*(Aq); ## unset file() and remove the current user\*(Aqs crontab .Ve .SS "error([string])" .IX Subsection "error([string])" Returns the last error encountered (usually during a file I/O operation). Pass an empty string to reset (calling \fBinit\fR will also reset it). .PP Example: .PP .Vb 2 \& print "The last error was: " . $ct\->error . "\en"; \& $ct\->error(\*(Aq\*(Aq); .Ve .SS "dump" .IX Subsection "dump" Returns a string containing the crontab file. .PP Example: .PP .Vb 2 \& ## show crontab \& print $ct\->dump; \& \& ## same as \*(Aqcrontab \-l\*(Aq except pretty\-printed \& $ct = new Config::Crontab; $ct\->read; print $ct\->dump; .Ve .SH "PACKAGE Config::Crontab::Block" .IX Header "PACKAGE Config::Crontab::Block" This section describes \fBConfig::Crontab::Block\fR objects (hereafter referred to as \fBBlock\fR objects). A \fBBlock\fR object is an abstracted way of dealing with groups of \fBcrontab\fR\|(5) lines. Depending on how \&\fBConfig::Crontab\fR parsed the file (see the \fBread\fR and \fBmode\fR methods in \fBConfig::Crontab\fR above), a block may consist of: .IP "a single line (e.g., a crontab event, environment setting, or comment)" 4 .IX Item "a single line (e.g., a crontab event, environment setting, or comment)" .PD 0 .ie n .IP "a ""paragraph"" of lines (a group of lines, each group separated by at least two newlines). This is the default parsing mode." 4 .el .IP "a ``paragraph'' of lines (a group of lines, each group separated by at least two newlines). This is the default parsing mode." 4 .IX Item "a paragraph of lines (a group of lines, each group separated by at least two newlines). This is the default parsing mode." .IP "the entire crontab file" 4 .IX Item "the entire crontab file" .PD .PP The default for \fBConfig::Crontab\fR is to read in \fIblock\fR (paragraph) mode. This allows you to group lines that have a similar purpose as well as order lines within a block (e.g., often you want an environment setting to take effect before certain cron commands execute). .PP An illustration may be helpful: .IP "\fBa crontab file read in block (paragraph) mode:\fR" 4 .IX Item "a crontab file read in block (paragraph) mode:" .Vb 4 \& Line Block Block Line Entry \& 1 1 1 ## grind disks \& 2 1 2 5 5 * * * /bin/grind \& 3 1 3 \& \& 4 2 1 ## backup reminder to joe \& 5 2 2 MAILTO=joe \& 6 2 3 5 0 * * Fri /bin/backup \& 7 2 4 \& \& 8 3 1 ## meeting reminder to bob \& 9 3 2 MAILTO=bob \& 10 3 3 30 9 * * Wed /bin/meeting .Ve .Sp Notice that each block has its own internal line numbering. Vertical space has been inserted between blocks to clarify block structures. Block mode parsing is the default. .IP "\fBa crontab file read in line mode:\fR" 4 .IX Item "a crontab file read in line mode:" .Vb 11 \& Line Block Block Line Entry \& 1 1 1 ## grind disks \& 2 2 1 5 5 * * * /bin/grind \& 3 3 1 \& 4 4 1 ## backup reminder to joe \& 5 5 1 MAILTO=joe \& 6 6 1 5 0 * * Fri /bin/backup \& 7 7 1 \& 8 8 1 ## meeting reminder to bob \& 9 9 1 MAILTO=bob \& 10 10 1 30 9 * * Wed /bin/meeting .Ve .Sp Notice that each line is also a block. You normally don't want to read in line mode unless you don't have paragraph breaks in your crontab file (the dumper prints a newline between each block; with each line being a block you get an extra newline between each line). .IP "\fBa crontab file read in file mode:\fR" 4 .IX Item "a crontab file read in file mode:" .Vb 11 \& Line Block Block Line Entry \& 1 1 1 ## grind disks \& 2 1 2 5 5 * * * /bin/grind \& 3 1 3 \& 4 1 4 ## backup reminder to joe \& 5 1 5 MAILTO=joe \& 6 1 6 5 0 * * Fri /bin/backup \& 7 1 7 \& 8 1 8 ## meeting reminder to bob \& 9 1 9 MAILTO=bob \& 10 1 10 30 9 * * Wed /bin/meeting .Ve .Sp Notice that there is only one block in file mode, and each line is a block line (but not a separate block). .SH "METHODS" .IX Header "METHODS" This section describes methods accessible from \fBBlock\fR objects. .SS "new([%args])" .IX Subsection "new([%args])" Creates a new \fBBlock\fR object. You may create \fBBlock\fR objects in any of the following ways: .IP "Empty" 4 .IX Item "Empty" .Vb 1 \& $event = new Config::Crontab::Block; .Ve .IP "Fully Populated" 4 .IX Item "Fully Populated" .Vb 4 \& $event = new Config::Crontab::Block( \-data => <<_BLOCK_ ); \& ## a comment \& 5 19 * * Mon /bin/fhe \-\-turn=dad \& _BLOCK_ .Ve .PP Constructor attributes available in the \fBnew\fR method take the same arguments as their method counterparts (described below), except that the names of the attributes must have a hyphen ('\-') prepended to the attribute name (e.g., 'lines' becomes '\-lines'). The following is a list of attributes available to the \fBnew\fR method: .IP "\fBdata\fR" 4 .IX Item "data" .PD 0 .IP "\fBlines\fR" 4 .IX Item "lines" .PD .PP If the \fB\-data\fR attribute is present in the constructor when other attributes are also present, the \fB\-data\fR attribute will override all other attributes. .PP Each of these attributes corresponds directly to its similarly-named method. .PP Examples: .PP .Vb 7 \& ## create an empty block object & populate it with the data method \& $block = new Config::Crontab::Block; \& $block\->data( <<_BLOCK_ ); ## via a \*(Aqhere\*(Aq document \& ## 2:05a Friday backup \& MAILTO=sysadmin@mydomain.ext \& 5 2 * * Fri /sbin/backup /dev/da0s1f \& _BLOCK_ \& \& ## create a block in the constructor (also via \*(Aqhere\*(Aq document) \& $block = new Config::Crontab::Block( \-data => <<_BLOCK_ ); \& ## 2:05a Friday backup \& MAILTO=sysadmin@mydomain.ext \& 5 2 * * Fri /sbin/backup /dev/da0s1f \& _BLOCK_ \& \& ## create an array of crontab objects \& my @lines = ( new Config::Crontab::Comment(\-data => \*(Aq## run bar\*(Aq), \& new Config::Crontab::Event(\-data => \*(Aq5 8 * * * /foo/bar\*(Aq) ); \& \& ## create a block object via lines attribute \& $block = new Config::Crontab::Block( \-lines => \e@lines ); \& \& ## ...or with lines method \& $block\->lines(\e@lines); ## @lines is an array of crontab objects .Ve .PP If bogus data is passed to the constructor, it will return \fIundef\fR instead of an object reference. If there is a possiblility of poorly formatted data going into the constructor, you should check the object variable for definedness before using it. .PP If the \fB\-data\fR attribute is present in the constructor when other attributes are also present, the \fB\-data\fR attribute will override all other attributes. .SS "data([string])" .IX Subsection "data([string])" Get or set a raw block. Internally, \fBBlock\fR passes its arguments to other objects for parsing when a parameter is present. .PP Example: .PP .Vb 2 \& ## re\-initialize this block \& $block\->data("## comment\en5 * * * * /bin/checkup"); \& \& print $block\->data; .Ve .PP Block data is terminated with a final newline. .SS "lines([\e@objects])" .IX Subsection "lines([@objects])" Get block data as a list of \fBConfig::Crontab::*\fR objects. Set block data using a list reference. .PP Example: .PP .Vb 2 \& $block\->lines( [ new Config::Crontab::Comment( \-data => "## run backup" ), \& new Config::Crontab::Event( \-data => "5 4 * * 1\-5 /sbin/backup" ) ] ); \& \& ## sorta like $block\->dump \& for my $obj ( $block\->lines ) { \& print $obj\->dump . "\en"; \& } \& \& ## a clumsy way to "unshift" a new event \& $block\->lines( [new Config::Crontab::Comment(\-data => \*(Aq## hi mom!\*(Aq), \& $block\->lines] ); \& \& ## the right way to add a new event \& $block\->first( new Config::Crontab::Comment(\-data => \*(Aq## hi mom!\*(Aq) ); \& print $_\->dump for $block\->lines; .Ve .SS "select([%criteria])" .IX Subsection "select([%criteria])" Returns a list of \fBEvent\fR, \fBEnv\fR, or \fBComment\fR objects from a block that match the specified criteria. Multiple criteria may be specified. .PP Field names should be preceded by a hyphen (though without a hyphen is acceptable too; we use hyphens to avoid the need for quoting keys and avoid potential bareword collisions). .PP If not criteria are specified, \fBselect\fR returns a list of all lines in the block (like \fBlines\fR). .PP Example: .PP .Vb 4 \& ## select all events \& for my $event ( $block\->select( \-type => \*(Aqevent\*(Aq) ) { \& print $event\->dump . "\en"; \& } \& \& ## select events that have the word \*(Aqfoo\*(Aq in the command \& for my $event ( $block\->select( \-type => \*(Aqevent\*(Aq, \-command_re => \*(Aqfoo\*(Aq) ) { \& print $event\->dump . "\en"; \& } .Ve .SS "remove(@objects)" .IX Subsection "remove(@objects)" Remove \fBConfig::Crontab::*\fR objects from this block. .PP Example: .PP .Vb 2 \& ## simple case: you need to get a handle on these objects first \& $block\->remove( $obj1, $obj2, $obj3 ); \& \& ## more complex: remove an event from a block by searching \& for my $event ( $block\->select( \-type => \*(Aqevent\*(Aq) ) { \& next unless $event\->command =~ /\ebbackup\eb/; ## look for backup command \& $block\->remove($event); last; ## and remove it \& } .Ve .ie n .SS "replace($oldobj, $newobj)" .el .SS "replace($oldobj, \f(CW$newobj\fP)" .IX Subsection "replace($oldobj, $newobj)" Replaces \fI\f(CI$oldobj\fI\fR with \fI\f(CI$newobj\fI\fR within a block. Returns \fI\f(CI$oldobj\fI\fR if successful, \fIundef\fR otherwise. .PP Example: .PP .Vb 5 \& ## replace $event1 with $event2 in this block. \& ## \*(Aq=>\*(Aq is the same as a comma (,) \& ($event1) = $block\->select(\-type => \*(Aqevent\*(Aq, \-command => \*(Aq/bin/foo\*(Aq); \& $event2 = new Config::Crontab::Event( \-data => \*(Aq5 2 * * * /bin/bar\*(Aq ); \& ok( $block\->replace($event1 => $event2) ); .Ve .SS "up($target_obj), down($target_obj)" .IX Subsection "up($target_obj), down($target_obj)" These methods move the \fBConfig::Crontab::*\fR object up or down within the block. .PP If the object is not a member of the block, it will be added to the block in the first position for \fBup\fR and it will be added to the block in the last position for \fBdown\fR. .PP Examples: .PP .Vb 1 \& $block\->up($event); ## move event up one position in the block \& \& ## add a new event at the end of the block \& $block\->down(new Config::Crontab::Event(\-data => \*(Aq5 2 * * Mon /bin/monday\*(Aq)); .Ve .SS "first(@target_obj), last(@target_obj)" .IX Subsection "first(@target_obj), last(@target_obj)" These methods move the \fBConfig::Crontab::*\fR object(s) to the first or last positions in the block. .PP If the object or objects are not members of the block, they will be added to the first or last part of the block respectively. .PP Examples: .PP .Vb 1 \& $block\->first($comment); ## move $comment to the first line in this block \& \& ## add these new events to the end of the block \& $block\->last( new Config::Crontab::Comment(\-data => \*(Aq## hi mom!\*(Aq), \& new Config::Crontab::Comment(\-data => \*(Aq## hi dad!\*(Aq), ); .Ve .ie n .SS "before($look_for, @obj), after($look_for, @obj)" .el .SS "before($look_for, \f(CW@obj\fP), after($look_for, \f(CW@obj\fP)" .IX Subsection "before($look_for, @obj), after($look_for, @obj)" These methods move the \fBConfig::Crontab::*\fR object(s) to the position immediately before or after the \fI\f(CI$look_for\fI\fR (or reference) object in the block. .PP If the objects are not members of the block, they will be added to the block before or after the reference object. If the reference object does not exist in the block, the objects will be moved (or added) to the beginning or end of the block respectively (much the same as \fBfirst\fR and \fBlast\fR). .PP .Vb 2 \& ## simple example \& $block\->after($event, $comment); ## move $comment after $event in this block .Ve .SS "active(boolean)" .IX Subsection "active(boolean)" Activates or deactivates an entire block. If no arguments are given, \&\fBactive\fR returns true but does nothing, otherwise the boolean used to activate or deactivate the block is returned. .PP If you have a series of related crontab lines you wish to comment out (or uncomment), you can use this handy shortcut to do it. You cannot deactivate \fBComment\fR objects (i.e., they will always be comments). .PP Example: .PP .Vb 1 \& $block\->active(0); ## deactivate this block .Ve .SS "nolog(boolean)" .IX Subsection "nolog(boolean)" This is (currently) a SuSE-specific extension. From \fB\fBcrontab\fB\|(5)\fR: .PP .Vb 3 \& If the uid of the owner is 0 (root), he can put a "\-" as first \& character of a crontab entry. This will prevent cron from writing a \& syslog message about this command getting executed. .Ve .PP \&\fBnolog\fR enables adds or removes this hyphen for a given cron event line (regardless of whether the user is \fIroot\fR or not). .PP Example: .PP .Vb 1 \& $block\->nolog(1); ## quiet all entries in this block .Ve .SS "flag(string)" .IX Subsection "flag(string)" Flags a block or an object inside a block with the specified data. The data you specify is completely up to you. This can be handy if you need to operate on many objects at once and don't want to risk pulling the rug out from under some (i.e., deleting numbered elements from a list changes the numbering of subsequent objects in the list, which is probably not what you want). .PP All normal query operations apply to \fB\-flag\fR attributes (e.g., \&\fB\-flag_re\fR, \fB\-flag_nre\fR, etc). .PP Example: .PP .Vb 7 \& ## delete every other event in this block \& my $count = 0; \& for my $event ( $block\->select( \-type => \*(Aqevent\*(Aq ) ) { \& $event\->flag(\*(Aqdeleteme!\*(Aq) \& if $count % 2 == 0; \& $count++; \& } \& \& ## delete all blocks marked as \*(Aqdeleteme!\*(Aq \& $block\->remove( $block\->select( \-flag => \*(Aqdeleteme!\*(Aq ) ); .Ve .SS "dump" .IX Subsection "dump" Returns a formatted string of the \fBBlock\fR object (recursively calling all its objects' dump methods). A \fBBlock\fR dump is newline terminated. .PP Example: .PP .Vb 1 \& print $block\->dump; .Ve .SH "PACKAGE Config::Crontab::Event" .IX Header "PACKAGE Config::Crontab::Event" This section describes \fBConfig::Crontab::Event\fR objects (hereafter \&\fBEvent\fR objects). A \fBEvent\fR object is an abstracted way of dealing with \fBcrontab\fR\|(5) lines that look like any of the following (see \&\fBcrontab\fR\|(5)): .IP "5 0 * 3,6,9,12 * /bin/quarterly_report" 4 .IX Item "5 0 * 3,6,9,12 * /bin/quarterly_report" .PD 0 .ie n .IP "0 2 * * Fri $HOME/bin/cake_reminder" 4 .el .IP "0 2 * * Fri \f(CW$HOME\fR/bin/cake_reminder" 4 .IX Item "0 2 * * Fri $HOME/bin/cake_reminder" .ie n .IP "@daily /bin/bar arg1 arg2" 4 .el .IP "\f(CW@daily\fR /bin/bar arg1 arg2" 4 .IX Item "@daily /bin/bar arg1 arg2" .IP "#30 10 12 * * /bin/commented out" 4 .IX Item "#30 10 12 * * /bin/commented out" .IP "5 4 * * * joeuser /bin/winkerbean" 4 .IX Item "5 4 * * * joeuser /bin/winkerbean" .PD .PP \&\fBEvent\fR objects are lines in the crontab file which trigger an event at a certain time (or set of times). This includes events that have been commented out. In \fBEvent\fR object terms, an event that has been commented out is \fIinactive\fR. Events that have not been commented out are \fIactive\fR. .SS "Terminology" .IX Subsection "Terminology" The following description will serve as a terminology guide for this class: .PP Given the following crontab event entry: .PP .Vb 1 \& 5 3 * Apr Sun /bin/rejoice .Ve .PP we define the following parts of the \fBEvent\fR object: .PP .Vb 3 \& 5 3 * Apr Sun /bin/rejoice \& \-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\- \& datetime command .Ve .PP We can break down the \fBdatetime\fR field into the following parts: .PP .Vb 3 \& 5 3 * Apr Sun \& \-\-\-\-\-\- \-\-\-\- \-\-\- \-\-\-\-\- \-\-\- \& minute hour dom month dow .Ve .PP We might also see an event with a \*(L"special\*(R" datetime part: .PP .Vb 3 \& @daily /bin/brush \-\-teeth \-\-feet \& \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& datetime command .Ve .PP This special datetime field can also be called 'special': .PP .Vb 3 \& @daily /bin/brush \-\-teeth \-\-feet \& \-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& special command .Ve .PP As of version 1.05, \fBCrontab\fR supports system crontabs, which adds an extra \fIuser\fR field: .PP .Vb 3 \& 5 3 * Apr Sun chris /bin/rejoice \& \-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\- \& datetime user command .Ve .PP This field is described in \fBcrontab\fR\|(5) on most systems. .PP These and other methods for accessing and manipulating \fBEvent\fR objects are described in subsequent sections. .SH "METHODS" .IX Header "METHODS" This section describes methods available to manipulate \fBEvent\fR objects' creation and attributes. .SS "new([%args])" .IX Subsection "new([%args])" Creates a new \fBEvent\fR object. You may create \fBEvent\fR objects in any of the following ways: .IP "Empty" 4 .IX Item "Empty" .Vb 1 \& $event = new Config::Crontab::Event; .Ve .IP "Partially Populated" 4 .IX Item "Partially Populated" .Vb 1 \& $event = new Config::Crontab::Event( \-minute => 0 ); .Ve .IP "Fully Populated" 4 .IX Item "Fully Populated" .Vb 3 \& $event = new Config::Crontab::Event( \-minute => 5, \& \-hour => 2, \& \-command => \*(Aq/bin/document my_proggie\*(Aq, ); .Ve .IP "System Event" 4 .IX Item "System Event" .Vb 4 \& $event = new Config::Crontab::Event( \-minute => 5, \& \-hour => 2, \& \-user => \*(Aqjoeuser\*(Aq, \& \-command => \*(Aq/bin/foo \-\-bar=blech\*(Aq, ); .Ve .IP "System Event" 4 .IX Item "System Event" .Vb 2 \& $event = new Config::Crontab::Event( \-data => \*(Aq30 3 * * 5,6 joeuser /bin/blech\*(Aq, \& \-system => 1, ); .Ve .PP Constructor attributes available in the \fBnew\fR method take the same arguments as their method counterparts (described below), except that the names of the attributes must have a hyphen ('\-') prepended to the attribute name (e.g., 'month' becomes '\-month'). The following is a list of attributes available to the \fBnew\fR method: .IP "\fB\-minute\fR" 4 .IX Item "-minute" .PD 0 .IP "\fB\-hour\fR" 4 .IX Item "-hour" .IP "\fB\-dom\fR" 4 .IX Item "-dom" .IP "\fB\-month\fR" 4 .IX Item "-month" .IP "\fB\-dow\fR" 4 .IX Item "-dow" .IP "\fB\-special\fR" 4 .IX Item "-special" .IP "\fB\-data\fR" 4 .IX Item "-data" .IP "\fB\-datetime\fR" 4 .IX Item "-datetime" .IP "\fB\-user\fR" 4 .IX Item "-user" .IP "\fB\-system\fR" 4 .IX Item "-system" .IP "\fB\-command\fR" 4 .IX Item "-command" .IP "\fB\-active\fR" 4 .IX Item "-active" .PD .PP Each of these attributes corresponds directly to its similarly-named method. .PP Examples: .PP .Vb 5 \& ## use datetime attribute; using a \*(Aqspecial\*(Aq string in \-datetime is \& ## ok, but the reverse is not true (using a standard datetime string \& ## in \-special) \& $event = new Config::Crontab::Event( \-datetime => \*(Aq@hourly\*(Aq, \& \-command => \*(Aq/bin/bar\*(Aq ); \& \& \& ## use special attribute \& $event = new Config::Crontab::Event( \-special => \*(Aq@hourly\*(Aq, \& \-command => \*(Aq/bin/bar\*(Aq ); \& \& \& ## use datetime attribute \& $event = new Config::Crontab::Event( \-datetime => \*(Aq5 * * * Fri\*(Aq, \& \-command => \*(Aq/bin/bar\*(Aq ); \& \& \& ## this is an error because \*(Aq5 * * * Fri\*(Aq is not one of the special \& ## datetime strings. Currently this does not throw an error, but \& ## behavior is undefined for an object initialized thusly \& $event = new Config::Crontab::Event( \-special => \*(Aq5 * * * Fri\*(Aq, \& \-command => \*(Aq/bin/bar\*(Aq ); \& \& \& ## create an inactive Event; default for datetime fields is \*(Aq*\*(Aq \& ## the result is the line: "#0 2 * * * /bin/foo" (notice \*(Aq#\*(Aq) \& $event = new Config::Crontab::Event( \-active => 0, \& \-minute => 0, \& \-hour => 2, ## 2 am \& \-command => \*(Aq/bin/foo\*(Aq ); \& ...time passes... \& $event\->active(1); ## now activate that event \& \& \& ## let the object do all the hard parsing \& $event = new Config::Crontab::Event( \-data => \*(Aq30 3 * * 5,6 /bin/blech\*(Aq ); \& ...time passes... \& $event\->hour(4); ## change the event from 3:30a to 4:30a .Ve .PP If bogus data is passed to the constructor, it will return \fIundef\fR instead of an object reference. If there is a possiblility of poorly formatted data going into the constructor, you should check the object variable for definedness before using it. .SS "A note about the datetime fields" .IX Subsection "A note about the datetime fields" \&\fBEvent\fR objects have several ways of setting the datetime fields: .PP .Vb 2 \& ## via the special method \& $event\->special(\*(Aq@daily\*(Aq); \& \& ## via datetime \& $event\->datetime(\*(Aq@daily\*(Aq); \& \& ## via datetime \& $event\->datetime(\*(Aq0 0 * * *\*(Aq); \& \& ## via datetime fields \& $event\->minute(0); \& $event\->hour(0); \& \& ## via data (takes the command part also) \& $event\->data(\*(Aq0 0 * * * /bin/foo\*(Aq); \& \& ## via the constructor at object instantiation time \& $event = new Config::Crontab::Event( \-special => \*(Aq@reboot\*(Aq ); .Ve .PP The standard datetime fields are: \fBminute\fR, \fBhour\fR, \fBdom\fR, \&\fBmonth\fR, and \fBdow\fR. If you set \fBdatetime\fR using a \fBspecial\fR field, or if you initialize an \fBEvent\fR object using a \fBspecial\fR datetime field, the standard datetime fields are reset to '*' and are invalid. .PP The special datetime field is a single field that takes the place of the 5 standard datetime fields (see \fBcrontab\fR\|(5) and \*(L"special\*(R"). Currently, if you set \fBspecial\fR via the \fBspecial\fR method, the standard datetime fields (e.g., \fBminute\fR, \fBhour\fR, etc.) are \fInot\fR reset; the standard datetime fields are reset to '*' if you set \&\fBspecial\fR via the \fBdatetime\fR method. .PP See other important information in the \fBdatetime\fR and \fBspecial\fR method descriptions below. .PP If the \fB\-data\fR attribute is present in the constructor when other attributes are also present, the \fB\-data\fR attribute will override all other attributes. .SS "minute([digits])" .IX Subsection "minute([digits])" Get or set the minute attribute of the \fBEvent\fR object. .PP Example: .PP .Vb 1 \& $event\->minute(30); \& \& print "This event will occur at " . $event\->minute . " minutes past the hour\en"; \& \& $event\->minute(40); \& \& print "Now it will occur 10 minutes later\en"; .Ve .PP Note from \fBcrontab\fR\|(5): .PP .Vb 3 \& Ranges of numbers are allowed. Ranges are two numbers separated with a \& hyphen. The specified range is inclusive. For example, 8\-11 for an \& \`\`hours\*(Aq\*(Aq entry specifies execution at hours 8, 9, 10 and 11. \& \& Lists are allowed. A list is a set of numbers (or ranges) separated by \& commas. Examples: \`\`1,2,5,9\*(Aq\*(Aq, \`\`0\-4,8\-12\*(Aq\*(Aq. \& \& Step values can be used in conjunction with ranges. Following a range \& with \`\`/\*(Aq\*(Aq specifies skips of the number\*(Aqs value through the \& range. For example, \`\`0\-23/2\*(Aq\*(Aq can be used in the hours field to specify \& command execution every other hour (the alternative in the V7 standard is \& \`\`0,2,4,6,8,10,12,14,16,18,20,22\*(Aq\*(Aq). Steps are also permitted after an \& asterisk, so if you want to say \`\`every two hours\*(Aq\*(Aq, just use \`\`*/2\*(Aq\*(Aq. .Ve .SS "hour([digits])" .IX Subsection "hour([digits])" Get or set the hour attribute of the \fBEvent\fR object. .PP Example: analogous to \fBminute\fR .PP Note from \fBcrontab\fR\|(5): see \fB/\*(L"minute\*(R"\fR. .SS "dom([digits])" .IX Subsection "dom([digits])" Get or set the day-of-month attribute of the \fBEvent\fR object. .PP Example: analogous to \fBminute\fR .PP Note from \fBcrontab\fR\|(5): .PP .Vb 6 \& Note: The day of a command\*(Aqs execution can be specified by two fields \-\- \& day of month, and day of week. If both fields are restricted (ie, aren\*(Aqt \& *), the command will be run when either field matches the current time. \& For example, \& \`\`30 4 1,15 * 5\*(Aq\*(Aq would cause a command to be run at 4:30 am on the 1st \& and 15th of each month, plus every Friday. .Ve .SS "month([string])" .IX Subsection "month([string])" Get or set the month. This may be a digit (1\-12) or a three character English abbreviated month string (Jan, Feb, etc.). .PP Note from \fBcrontab\fR\|(5): .PP .Vb 3 \& Names can also be used for the \`\`month\*(Aq\*(Aq and \`\`day of week\*(Aq\*(Aq fields. Use \& the first three letters of the particular day or month (case doesn\*(Aqt mat\- \& ter). Ranges or lists of names are not allowed. .Ve .SS "dow([string])" .IX Subsection "dow([string])" Get or set the day of week. .PP Example: analogous to \fBminute\fR .PP Note from \fBcrontab\fR\|(5): see the \fB/\*(L"month\*(R"\fR entry above. .SS "special([string])" .IX Subsection "special([string])" Get or set the special datetime field. .PP The special datetime field is one of (from \fBcrontab\fR\|(5)): .PP .Vb 10 \& string meaning \& \-\-\-\-\-\- \-\-\-\-\-\-\- \& @reboot Run once, at startup. \& @yearly Run once a year, "0 0 1 1 *". \& @annually (sames as @yearly) \& @monthly Run once a month, "0 0 1 * *". \& @weekly Run once a week, "0 0 * * 0". \& @daily Run once a day, "0 0 * * *". \& @midnight (same as @daily) \& @hourly Run once an hour, "0 * * * *". .Ve .PP If you set a datetime via \fBspecial\fR, this will override anything in the other standard datetime fields. .PP While you may use a special datetime string as an argument to the \&\fBdatetime\fR method, you may \fInot\fR use a standard datetime string in the \fBspecial\fR method. Currently there is no error checking on this field, but behavior is undefined. .PP The \fBdatetime\fR method will return the \fBspecial\fR value in preference to any other standard datetime fields. That is, if \fBspecial\fR has a value (e.g., '@reboot', etc.) it will be returned in all methods that return aggregate event data (e.g., \fBdatetime\fR, \fBdump\fR, \fBdata\fR, etc.). If \fBspecial\fR is false, the standard datetime fields will be returned instead. Thus, you should always check the value of \&\fBspecial\fR before using any of the standard datetime fields: .PP .Vb 3 \& if( $event\->special ) { \& print $event\->special . "\en"; \& } \& \& ## use standard datetime elements \& else { \& print $event\->minute . " " . $event\->hour ... \& } .Ve .PP If you're presenting the entire datetime field formatted, use the \&\fBdatetime\fR method (and then you don't have to do any checks on \&\fBspecial\fR): .PP .Vb 3 \& ## will print the special datetime value if set, \& ## standard datetime fields otherwise \& print $event\->datetime . "\en"; .Ve .SS "data([string])" .IX Subsection "data([string])" Get or set the raw event line. .PP Internally, this is how the main \fBConfig::Crontab\fR class does its parsing: it iterates over the crontab file and hands each line off to the \fBdata\fR method for further parsing. .PP Example: .PP .Vb 1 \& $event\->data("#0 2 * * * /bin/foo"); \& \& ## prints "inactive (/bin/foo): 0 2 * * *"; \& print ( $event\->active ? \*(Aq\*(Aq : \*(Aqin\*(Aq ) . \*(Aqactive \*(Aq \& . \*(Aq(\*(Aq . $event\->command . \*(Aq): " \& . $event\->datetime; .Ve .SS "datetime([string])" .IX Subsection "datetime([string])" Get or set the datetime fields of an event. .PP Possible datetime fields are either a special datetime format (e.g., \&\f(CW@daily\fR, \f(CW@weekly\fR, etc) \fBor\fR a standard datetime format (e.g., \*(L"0 2 * * Mon\*(R" is standard). .PP \&\fBdatetime\fR is often a convenient shortcut for parsing a datetime field if you're not precisely sure what's in it (but are sure that it's either a special datetime field or a standard datetime field): .PP .Vb 1 \& $event\->datetime($some_string); .Ve .PP While you may pass a special datetime field into \fBdatetime\fR, you may \&\fBnot\fR pass a standard field into the \fBspecial\fR method. Currently, the object will not complain, and may even work in most cases, but the behavior is undefined and will likely become more strict in the future. .SS "user([string])" .IX Subsection "user([string])" Get or set the user part of a \fIsystem\fR \fBevent\fR object. .PP Example: .PP .Vb 1 \& $event\->user(\*(Aqjoeuser\*(Aq); .Ve .PP The \fBuser\fR field is only accessible when the crontab object was created or parsed with \fBsystem\fR mode enabled (see \*(L"system\*(R" above). .SS "system([boolean])" .IX Subsection "system([boolean])" When set, will parse a \fB\-data\fR string looking for a username before the command as described in \fBcrontab\fR\|(5). .PP Example: .PP .Vb 2 \& $event\->system(1); \& $event\->data(\*(Aq0 2 * * * joeuser /bin/foo \-\-args\*(Aq); .Ve .PP This will set the user as 'joeuser' and the command as '/bin/foo \&\-\-args'. Notice that if you pass bad data, the \fBEvent\fR parser really can't help since the \fIuser\fR (including '/') syntax is now supported as of version 1.05: .PP .Vb 2 \& $event = new Config::Crontab::Event( \-data => \*(Aq2 5 * * * /bin/foo \-\-args\*(Aq, \& \-system => 1 ); .Ve .PP The \fBEvent\fR object will have '/bin/foo' as its user and '\-\-args' as its command. While things will usually work out when you write to file, you definitely won't get what you're expecting if you grok the \&\fIcommand\fR field. .SS "command([string])" .IX Subsection "command([string])" Get or set the command part of a \fBEvent\fR object. .PP Example: .PP .Vb 1 \& $event\->command(\*(Aq/bin/foo with args here\*(Aq); .Ve .SS "active([boolean])" .IX Subsection "active([boolean])" Get or set whether the \fBEvent\fR object is active. In practical terms, this simply inserts a pound sign before the datetime fields when accessing the \fBdump\fR method. It is only used implicitly in \fBdump\fR, but may be accessed separately whenever convenient. .PP .Vb 1 \& print ( $event\->active ? \*(Aq\*(Aq : \*(Aq#\*(Aq ) . $event\->data . "\en"; .Ve .PP is the same as: .PP .Vb 1 \& print $event\->dump . "\en"; .Ve .SS "dump" .IX Subsection "dump" Returns a formatted string of the \fBEvent\fR object. This method is called implicitly when flushing to disk in \fBConfig::Crontab\fR. It is not newline terminated. .PP Example: .PP .Vb 1 \& print $event\->dump . "\en"; .Ve .SH "PACKAGE Config::Crontab::Env" .IX Header "PACKAGE Config::Crontab::Env" This section describes \fBConfig::Crontab::Env\fR objects (hereafter \&\fBEnv\fR objects). A \fBEnv\fR object is an abstracted way of dealing with crontab lines that look like any of the following (see \fBcrontab\fR\|(5)): .PP .Vb 1 \& name = value .Ve .PP From \fBcrontab\fR\|(5): .PP .Vb 7 \& the spaces around the equal\-sign (=) are optional, and any \& subsequent non\-leading spaces in value will be part of the value \& assigned to name. The value string may be placed in quotes \& (single or double, but matching) to preserve leading or trailing \& blanks. The name string may also be placed in quote (single or \& double, but matching) to preserve leading, traling or inner \& blanks. .Ve .PP Like \fBEvent\fR objects, \fBEnv\fR objects may be \fIactive\fR or \fIinactive\fR, the difference being an \fIinactive\fR \fBEnv\fR object is commented out: .PP .Vb 1 \& #FOO=bar .Ve .SS "Terminology" .IX Subsection "Terminology" Given the following crontab environment line: .PP .Vb 1 \& MAILTO=joe .Ve .PP we define the following parts of the \fBEnv\fR object: .PP .Vb 3 \& MAILTO = joe \& ====== ============ ===== \& name (not stored) value .Ve .PP These and other methods for accessing and manipulating \fBEvent\fR objects are described in subsequent sections. .SH "METHODS" .IX Header "METHODS" .SS "new([%args])" .IX Subsection "new([%args])" Creates a new \fBEnv\fR object. You may create \fBEnv\fR objects any of the following ways: .IP "Empty" 4 .IX Item "Empty" .Vb 1 \& $env = new Config::Crontab::Env; .Ve .IP "Partially Populated" 4 .IX Item "Partially Populated" .Vb 1 \& $env = new Config::Crontab::Env( \-value => \*(Aqjoe\*(Aq ); .Ve .IP "Fully Populated" 4 .IX Item "Fully Populated" .Vb 2 \& $env = new Config::Crontab::Env( \-name => \*(AqFOO\*(Aq, \& \-value => \*(Aqblech\*(Aq ); .Ve .PP Constructor attributes available in the \fBnew\fR method take the same arguments as their method counterparts (described below), except that the names of the attributes must have a hyphen ('\-') prepended to the attribute name (e.g., 'value' becomes '\-value'). The following is a list of attributes available to the \fBnew\fR method: .IP "\fB\-name\fR" 4 .IX Item "-name" .PD 0 .IP "\fB\-value\fR" 4 .IX Item "-value" .IP "\fB\-data\fR" 4 .IX Item "-data" .IP "\fB\-active\fR" 4 .IX Item "-active" .PD .PP Each of these attributes corresponds directly to its similarly-named method. .PP Examples: .PP .Vb 3 \& ## use name and value \& $env = new Config::Crontab::Env( \-name => \*(AqMAILTO\*(Aq, \& \-value => \*(Aqjoe@schmoe.org\*(Aq ); \& \& ## parse a whole string \& $env = new Config::Crontab::Env( \-data => \*(AqMAILTO=joe@schmoe.org\*(Aq ); \& \& ## use name and value to create an inactive object \& $env = new Config::Crontab::Env( \-active => 0, \& \-name => \*(AqMAILTO\*(Aq, \& \-value => \*(Aqmike\*(Aq, ); \& $env\->active(1); ## now activate it \& \& ## create an object that will unset the environment variable \& $env = new Config::Crontab::Env( \-name => \*(AqMAILTO\*(Aq ); \& \& ## another way \& $env = new Config::Crontab::Env( \-data => \*(AqMAILTO=\*(Aq ); \& \& ## yet another way \& $env = new Config::Crontab::Env; \& $env\->name(\*(AqMAILTO\*(Aq); .Ve .PP If bogus data is passed to the constructor, it will return \fIundef\fR instead of an object reference. If there is a possiblility of poorly formatted data going into the constructor, you should check the object variable for definedness before using it. .PP If the \fB\-data\fR attribute is present in the constructor when other attributes are also present, the \fB\-data\fR attribute will override all other attributes. .SS "name([string])" .IX Subsection "name([string])" Get or set the object name. .PP Example: .PP .Vb 1 \& $env\->name(\*(AqMAILTO\*(Aq); .Ve .SS "value([string])" .IX Subsection "value([string])" Get or set the value associated with the name attribute. .PP Example: .PP .Vb 1 \& $env\->value(\*(Aqtom@tomorrow.org\*(Aq); \& \& print "The value for " . $env\->name . " is " . $env\->value . "\en"; .Ve .SS "data([string])" .IX Subsection "data([string])" Get or set a raw environment line. .PP Example: .PP .Vb 1 \& $env\->data(\*(AqMAILTO=foo@bar.org\*(Aq); \& \& print "This object says: " . $env\->data . "\en"; .Ve .SS "active([boolean])" .IX Subsection "active([boolean])" Get or set whether the \fBEnv\fR object is active. In practical terms, this simply inserts a pound sign before the \fBname\fR field when accessing the \fBdump\fR method. It may be used whenever convenient. .PP .Vb 1 \& print $env\->dump . "\en"; .Ve .PP is the same as: .PP .Vb 1 \& print ( $env\->active ? \*(Aq\*(Aq : \*(Aq#\*(Aq ) . $env\->data . "\en"; .Ve .SS "dump" .IX Subsection "dump" Returns a formatted string of the \fBEnv\fR object. This method is called implicitly when flushing to disk in \fBConfig::Crontab\fR. It is not newline terminated. .PP .Vb 1 \& print $env\->dump . "\en"; .Ve .SH "PACKAGE Config::Crontab::Comment" .IX Header "PACKAGE Config::Crontab::Comment" This section describes \fBConfig::Crontab::Comment\fR objects (hereafter \&\fBComment\fR objects). A \fBComment\fR object is an abstracted way of dealing with crontab comments and whitespace (blank lines or lines that consist only of whitespace). .SH "METHODS" .IX Header "METHODS" .SS "new([%args])" .IX Subsection "new([%args])" Creates a new \fBComment\fR object. You may create \fBComment\fR objects in any of the following ways: .IP "Empty" 4 .IX Item "Empty" .Vb 1 \& $comment = new Config::Crontab::Comment; .Ve .IP "Populated" 4 .IX Item "Populated" .Vb 1 \& $comment = new Config::Crontab::Comment( \-data => \*(Aq# this is a comment\*(Aq ); .Ve .Sp and an alternative: .Sp .Vb 1 \& $comment = new Config::Crontab::Comment( \*(Aq# this is a constructor shortcut\*(Aq ); .Ve .PP Constructor attributes available in the \fBnew\fR method take the same arguments as their method counterparts (described below), except that the names of the attributes must have a hyphen ('\-') prepended to the attribute name (e.g., 'data' becomes '\-data'). The following is a list of attributes available to the \fBnew\fR method: .IP "\fB\-data\fR" 4 .IX Item "-data" .PP Each of these attributes corresponds directly to its similarly-named method. .PP Examples: .PP .Vb 2 \& ## using data \& $comment = new Config::Crontab::Comment( \-data => \*(Aq## a nice comment\*(Aq ); \& \& ## using data method \& $comment = new Config::Crontab::Comment; \& $comment\->data(\*(Aq## hi Mom!\*(Aq); .Ve .PP If bogus data is passed to the constructor, it will return \fIundef\fR instead of an object reference. If there is a possiblility of poorly formatted data going into the constructor, you should check the object variable for definedness before using it. .PP As a shortcut, you may omit the \fB\-data\fR label and simply pass the comment itself: .PP .Vb 1 \& $comment = new Config::Crontab::Comment(\*(Aq## this space for rent or lease\*(Aq); .Ve .SS "data([string])" .IX Subsection "data([string])" Get or set a comment. .PP Example: .PP .Vb 1 \& $comment\->data(\*(Aq## this is not the comment you are looking for\*(Aq); .Ve .SS "dump" .IX Subsection "dump" Returns a formatted string of the \fBComment\fR object. This method is called implicitly when flushing to disk in \fBConfig::Crontab\fR. It is not newline terminated. .SH "CAVEATS" .IX Header "CAVEATS" .IP "\(bu" 4 Thanks to alert reader \*(L"Kirk\*(R" (no lastname given), we learn that some versions of Debian linux's \*(L"crontab \-l\*(R" does not strip the internal \&\fBcrontab\fR\|(1) comments (e.g., \*(L"\s-1DO NOT EDIT THIS FILE\*(R"\s0 and subsequent meta-data) at the start of user crontabs. .Sp This means that if you use Config::Crontab to edit a user's crontab file, those three headers will be added to the Config::Crontab object, and written back out again, and \fBcrontab\fR\|(1) will add its own comments, effectively adding 3 comment lines each time you edit the crontab. .Sp You may use this little heuristic as a starting point for stripping those comments: .Sp .Vb 2 \& my $ct = new Config::Crontab; \& $ct\->read; \& \& ## make "crontab \-l | crontab \-" idempotent for Debian \& for my $line ( grep { defined } ($ct\->select(\-type => \*(Aqcomment\*(Aq))[0..2] ) { \& if( $line\->data =~ qr(^# (?:DO NOT EDIT|[\e(]\eS+ installed|[\e(]Cron version)) ) { \& $ct\->remove($line); \& } \& } \& \& ... \& \& $ct\->write; .Ve .IP "\(bu" 4 As of version 1.05, \fBConfig::Crontab\fR supports the user field (with optional ':group' and '/') via the \fB\-system\fR initialization parameter, \fBsystem\fR \fBEvent\fR method, or \fBuser\fR \&\fBEvent\fR method and \fBEvent\fR initialization parameter. .IP "\(bu" 4 You will not get good results adding non\-\fBBlock\fR objects to a \&\fBCrontab\fR object directly: .Sp .Vb 1 \& $ct\->last( new Config::Crontab::Event(\-data => \*(Aq1 2 3 4 5 /bin/friday\*(Aq) ); .Ve .Sp This doesn't do anything (and shouldn't). You should be adding \&\fBBlock\fR objects to the \fBCrontab\fR object instead: .Sp .Vb 2 \& $block\->last(new Config::Crontab::Event(\-data => \*(Aq1 2 3 4 5 /bin/friday\*(Aq)); \& $ct\->last($block); .Ve .Sp or the slightly more economical: .Sp .Vb 1 \& $ct\->last( new Config::Crontab::Block(\-data => \*(Aq1 2 3 4 5 /bin/friday\*(Aq) ); .Ve .Sp This is nice since the \fBBlock\fR constructor parses its \fB\-data\fR parameter as raw data and creates all the necessary objects to populate itself. The downside of this last approach is that you don't get a handle to your block if you need to make later changes. It can be easily got, however, since we appended it to the end (using \&\fBlast\fR) of the \fBCrontab\fR object: .Sp .Vb 1 \& $block = ($ct\->blocks)[\-1]; .Ve .SH "TODO" .IX Header "TODO" .IP "\(bu" 4 a better query language that would allow for boolean operators and more complexity (\s-1SQL,\s0 maybe? I've seen that in one of Ken William's modules using Parse::RecDescent) .IP "\(bu" 4 would be cool to use some fancier datetime parsers that can guess when an event will occur and allow that in our select methods. I've seen one of those on \s-1CPAN\s0 but didn't look too closely. Maybe someone will use both if they need both. .IP "\(bu" 4 need copy constructors (and clone method) .IP "\(bu" 4 need to be more strict about strict (it should do more things, enable more regex checks on data, etc.) .IP "\(bu" 4 some pretty-print options for \fBdump\fR .IP "\(bu" 4 alternative crontab syntax support (e.g. SysV-syntax used by Solaris doesn't support weekday 7 or 3\-letter month and day name abbreviations) .Sp \&\fBConfig::Crontab\fR will support SysV-syntax since it is a proper subset of Vixie cron syntax, but you will need to necessarily perform your own syntax checking and omit elements unique to Vixie cron in your \s-1UI.\s0 .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" .IP "\(bu" 4 Juan Jose Natera Abreu (naterajj@yahoo.com) for unsafe POSIX::tmpnam alert; now using File::Temp. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBcron\fR\|(8), \fBcrontab\fR\|(1), \fBcrontab\fR\|(5) .SH "AUTHOR" .IX Header "AUTHOR" Scott Wiersdorf, .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2007 by Scott Wiersdorf .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.