The next code is used to get a filepath and check if exists and can be read; otherwise the value will be switched to a custom one:
use strict;
use warnings;
[...]
sub checkFilePath{
my ($args) = @_;
my $checkingPath = $args->{path};
my $checkingCustomPath = $args->{customPath};
my $canBeRead = 1;
if ($checkingPath) {
if (!(-e "$checkingPath")) {
print "[WARN] File $checkingPath doesn't exist.\n";
$canBeRead = 0;
} elsif (!(-f "$checkingPath")) {
print "[WARN] $checkingPath is not a file.\n";
$canBeRead = 0;
} elsif (!(-r "$checkingPath")) {
print "[WARN] File $checkingPath can't be read.\n";
$canBeRead = 0;
}
}
if (!($canBeRead)) {
# Testing custom regex file path
# If doesn't exist, it will try to use custom file or the script will die
die "[ERR] Custom file $checkingCustomPath doesn't exist\n" if (!(-e $checkingCustomPath));
die "[ERR] Custom file $checkingCustomPath is not a file\n" if (!(-f $checkingCustomPath));
die "[ERR] Custom file $checkingCustomPath cannot be read\n" if (!(-r $checkingCustomPath));
return $checkingCustomPath;
}
return $checkingPath;
}
[...]
$logPath = checkFilePath({
path => $logPath,
customPath => $customLogPath
});
I was wondering if there is a way to modify this code to update $logPath only with a subroutine call, like:
# $logPath = '/tmp/thisfiledoesntexist.txt'
checkFilePath({
path => $logPath,
customPath => $customLogPath
});
# $logPath now has a valid filepath, which is the same as $customLogPath