http://www.perlmonks.org?node_id=874389


in reply to Resizing MRTG (RRDTool) logs en-masse

my @rrds = grep { /.*rrd$/ && -f "$logsdir/$_" } readdir DIR;

Why match zero or more characters in  /.*rrd$/ when  /rrd$/ would match the same thing with less work?    Perhaps you meant  /\.rrd$/?



$cmd = qq|$mv resize.rrd $logsdir/$rrd|; print "\tRenaming resized file, executing $cmd\n"; system($cmd) == 0 or die "Could not execute $cmd:$!\n";

Why not just use Perl's built-in rename function?

print "\tRenaming resized file, executing $cmd\n"; rename 'resize.rrd', "$logsdir/$rrd" or die "Could not rename +resize.rrd:$!\n";

Replies are listed 'Best First'.
Re^2: Resizing MRTG (RRDTool) logs en-masse
by McDarren (Abbot) on Nov 30, 2010 at 01:33 UTC
    Thanks for the feedback, both valid points.

    heh... I completely forgot about the rename function ;-)

      Also,  /.*rrd$/ will match both of the strings  "rrd" and  "rrd\n" so perhaps you should use  /.*rrd\z/ instead.

      Or perhaps even:  'rrd' eq substr( $_, -3 )

        It would be pretty unusual to have a file name that ends with \n, so I see that as an edge case that's probably not worth worrying about.

        It's probably better to have a check after the call to RRDs::info, so I've inserted:

        my $info = RRDs::info "$logsdir/$rrd"; # Check to ensure we actually have a valid rrd file unless ($info->{filename}) { print qq|"$logsdir/$rrd" doesn't appear to be a valid rrd log, + skipping\n|; next; }
        Which does the trick...
        $ touch foo.rrd $ ./rrd-resize.pl Starting, found 1 rrd files Processing foo.rrd "./foo.rrd" doesn't appear to be a valid rrd log, skipping Finished, processed 1 files in 0.00 seconds