Well, you get the idea.
$md5 = `md5sum $filename.new | awk '{ print $1 }'`;
$md5 =~ s/\n$//;
if ($md5 ne $origmd5) {
system("mv $filename $filename.old");
system("mv $filename.new $filename");
…
}
Sure, Perl can pretend to be a glorified shell script, but there are perfectly well-functioning internal functions for these things.
Let's ignore the non-portability of the
md5sum command for the time being, and also avoid shaving that awk call down with cut and an enclosing echo -n $(…) - we're here for the Perl, not the shell, right?The above
system() calls can easily be replaced with the following to save yourself a few unnecessary forks:Note the slight refinement of using
rename($filename,"$filename.old") and
rename("$filename.new",$filename);
and to avoid clobbering the file. But the documentation for rename() suggests that we use move() from File::Copy instead:Similarly, there are nice built-in functions for many other frequent victims of
use File::Copy qw(mv);
mv($filename,"$filename.old") and
mv("$filename.new",$filename);
system(), e.g.:And of course there is a bunch of more or less helpful modules on CPAN (in addition to the already mentioned
chmod
chgrp + chown
link
mkdir
rmdir
symlink
unlink
File::Copy), e.g. File::Path.