If you've ever used exception handling in another language, it's not too unfamiliar in Perl. You can use
eval to catch an exception thrown with
die:
# code to open ERRLOG here
eval {
die "There's no way around this!";
};
if ($@) {
print ERRLOG "Caught: $@!\n";
}
As for the directory deletion and warning, your code will always log a successful deletion, even if it fails. Try this:
if (rmdir ($_)) {
open(LOG, ">>$logfile") or warn "discarding logfile output\n";
print LOG "Directory: $_ - has been deleted.\n";
close (LOG) or warn "Can't close: $!";
} else {
warn "Cannot delete directory: $_ ($!)";
}