Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The two key suggestions above that are most important: (1) create a minimal script that focuses on the main problem (what differs between interactive use and cron-job use), and (2) use strict.

Apart from that, your script suffers from too much repetition of path information, ugly use of "." for string concatenation where a single double-quoted string will do, too many lines of code that could be removed by using a couple more core modules (e.g. POSIX, which provides the very handy "strftime()" function), and probably some errors that you aren't aware of.

For example, you have some lines like this:

$geoTiff_delete_all = </work/locust1/swarmsdata/RasterData/greening/*. +tif>; $zip_delete_all = </work/locust1/swarmsdata/RasterData/greening/*.zip> +;
Are you aware that if there are many *.tif and *.zip files, only one file name will be assigned to each of those variables? Is that your intention? Try this set of shell commands to see what I'm talking about:
cd /tmp touch foo1.foo foo2.foo foo3.foo perl -le '$f=<*.foo>; print "scalar: $f"; @f=<*.foo>; print "array: @f +"'

UPDATE: Just to give you an idea of how coding style relates to ease of coding and maintenance, the following snippet of 48 lines should be functionally the same as (or better than) the first 186 lines of the OP perl script; I'm using strictures, consistent indentation, simplest possible quoting, no repetition of literal strings, fewer variables, no redundant comments, and a "for" loop that eliminates several lines of repeated code:

#!/usr/bin/perl use strict; use warnings; use POSIX; use Archive::Extract; $Archive::Extract::PREFER_BIN=1; my $basedir = '/work/locust1/swarmsdata/RasterData'; my $greendir = "$basedir/greening"; my $dekadalGet = "$greendir/dekadalGet.txt"; my $dekadalNotGet = "$greendir/dekadalNotGet.txt"; my $user = 'xxxx'; my $pass = 'xxxx'; my $timeStamp = strftime( "%Y-%m-%d %H:%M:%S", localtime()); print "\n$timeStamp ****STARTING MODIS GREENING COLLECTION ROUTINE**** +\n\n"; for my $typ ( 'zip', 'tif' ) { print " -Checking dir for residual $typ file(s). \n"; my @delete_all = <$greendir/*.$typ>; if ( @delete_all ) { print " - $typ file(s) found purging directory. \n"; unlink @delete_all or die "$greendir/*.$typ - cannot be delete +d."; print " -Purge complete.\n\n"; } else { print " -No $typ file(s) found.\n"; } } print " -Checking dir for residual dekadalNotGet file.\n"; if ( -e $dekadalNotGet ) { print " -dekadalNotGet file(s) found purging directory.\n"; unlink $dekadalNotGet or die "$dekadalNotGet - cannot be deleted." +; print " -Purge complete.\n\n"; } else { print " -No $dekadalNotGet file found.\n"; } my ( $yr, $mo, $dy, $hr ) = ( $timeStamp =~ /(\d{4})-(\d\d)-(\d\d) (\d +\d):/ ); if ( $hr eq '06' and $dy =~ /[012]1/ ) { open( my $fh, '>', $dekadalGet ) or die "$dekadalGet: $!\n"; print $fh "$yr$mo$dy\n"; close $fh; print " -Collection day confirmed: added '$yr$mo$dy' to collection + file\n\n"; } print " **Starting dekadal collection procedure:**\n\n"; ...

I stopped at line 186 in your script ("Starting ... collection procedure") because after that point it was less clear to me what you were really trying to do (i.e. I'm not sure the OP code was actually doing what you intended). But I would expect you could get a similar (4-to-1) reduction for the rest of the script.


In reply to Re: Perl Module Not Working In Crontab by graff
in thread Perl Module Not Working In Crontab by dailyoliver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found