kirkbrown has asked for the wisdom of the Perl Monks concerning the following question:

Here's some code I inherited. I am trying to use this code to accomplish the same task on a different file. I want to use GetOpts::STD in place of the hard coding. My question is how do I pass the "file info" into the GetOpts::STD from a cron job? Also, any assistance redoing this code will help. Thanks!

#!/user/perl5/bi/perl require "getopt.pl" Getopt(i"); $file = "usr/local/home/pfuser/CURRENT_54_USERS"; # I want to remove t +his hard coding and use # the Getopts::STD, but how do I p +ass info # from a cron job? @sids = (); @sindinfo = (); $numbers = 0; $DATE = 'date + %Y%m%d'; #Is this a Unix call? If so, how can I use P +erl for this? chomp $DATE; if (! open(USERFILE, $file)) { die "could not open $file.\n"; } while (<USERFILE>) { $curline = $_; chomp($curline); push(@sids, $curline); push(@sidinfo, " "); numbers++ } close USERFILE; Sfile - $opt_i; if (! open(INFILE, $file)) {die "could not open $file. \n"; } while(<INFILE>) { $curline = $_; chomp($curline); $startpos = 0; $length = index $curline, " "; $checksid = substr $curline, $startpos, $length; $startpos = $length + 1; $cheinfoadd = substr $curline, $startpos; $foundsid = 0; $index = 0; while ( $index < $numbers ) { if ( $sids[$index] eq $checksid ) { $sidinfo[$index] = $checkinfoadd; } $index++; } } close INFILE;
MORE CODE FOLLOWS…..

Replies are listed 'Best First'.
Re: Using GetOpts::STD
by gmargo (Hermit) on Nov 08, 2009 at 20:44 UTC

    $DATE = 'date + %Y%m%d'; #Is this a Unix call? If so, how can I use Perl for this?

    Use the following equivalent instead:

    use POSIX qw(strftime); my $DATE = strftime("%Y%m%d", localtime()); # or use gmtime()


    My question is how do I pass the "file info" into the GetOpts::STD from a cron job?

    To add a -f option with an argument (filename), modify Anonymous Monk's getopt to:

    getopt('if:',\%opts);

    A crontab entry can include arguments. Example:

    00 22 * * * /usr/local/bin/sid_proc.pl -f /usr/local/home/pfuser/CURRE +NT_54_USERS

    However, I generally run a shell script out of cron, and the shell script contains the arguments and calls the program.

    00 22 * * * /usr/local/bin/sid_batch.sh

    Where sid_batch.sh contains:

    #!/bin/sh /usr/local/bin/sid_proc.pl -f /usr/local/home/pfuser/CURRENT_54_USERS /usr/local/bin/sid_proc.pl -f /usr/local/home/pfuser/CURRENT_55_USERS /usr/local/bin/sid_proc.pl -f /usr/local/home/pfuser/CURRENT_56_USERS

Re: Using GetOpts::STD
by Anonymous Monk on Nov 08, 2009 at 20:04 UTC
Re: Using GetOpts::STD
by graff (Chancellor) on Nov 09, 2009 at 02:22 UTC
    In response to this question:
    how do I pass info from a cron job?

    As indicated in a previous reply, you can put specific command line args into the crontab entry that runs the perl script (which then looks at @ARGV). Or you can have the crontab entry run some other script that sets some environment variable(s) and then runs the perl script in question (which then looks at %ENV).

    But the thing to keep in mind is that each cron table entry is an invariant command line that is run at some regular interval, so whether a particular value (like a disk path) is in the crontab entry or is inside the perl script (or some other intermediate script), it's "hard-coded" in any case.

    If you don't want something like a specific path hard-coded in the perl script because it's likely to vary from one run to the next, then you probably don't want that thing hard-coded in the crontab entry either.

    Instead, you want some logic (probably within the perl script in question) that will apply whatever rules and/or searches, etc are needed to work out the details for whatever parameter is supposed to vary from one run to the next.