Reputation: 3222
I am trying to delete old log files from a directory.
Logic here is, if LOG directory doesn't exists create it; else delete files from LOG directory which is been created earlier.
...
use autodie;
my $logpath = "/home/Vinod/Test/LOG/";
if ( !-d $logpath ){
make_path $logpath or die "Failed to create path: $logpath";
} else {
unlink glob "$logpath/*.log";
}
...
So when the directory been already created and its empty I am getting below warning -
Can't unlink(): at script.pl line 41
How can I avoid this warning message when the directory already exists and empty. TIA.
Upvotes: 1
Views: 459
Reputation: 36
Comment the autodie
module. For creating the directory use mkdir
, and for deleting the directory use rmtree
. See below coding
use strict;
use warnings;
use File::Slurp;
use IO::All;
use File::Path;
use Win32;
#use autodie;
my $logpath = "G:/perl/StackOverflow/LOG";
if (!-d $logpath ){
mkdir($logpath);
} else {
unlink glob "$logpath/*.log";
rmtree $logpath;
}
Upvotes: 1
Reputation: 386386
You are using an old version of the autodie module. Upgrade it to avoid this bug.
$ perl -e'use autodie; unlink "nonexistent.*"; CORE::say "ok"'
Can't unlink(): at -e line 1
$ cpan autodie
...
TODDR/autodie-2.32.tar.gz
/usr/bin/make install -- OK
$ perl -e'use autodie; unlink "nonexistent.*"; CORE::say "ok"'
ok
Upvotes: 3
Reputation: 240314
The simplest way would be to replace
unlink glob "$logpath/*.log";
with
my @files = glob "$logpath/*.log";
unlink @files if @files;
Upvotes: 5