Dan Jacobson said:
I can get the shell's "set -u" protection in perl with "use strict",
but what about the shells' "set -e"? Must I everywhere add "or die"s
to chdirs etc., why can't I turn it on for the whole program with one
line at the top?
The 'set -e' option exits from the shell if any invoked commands exit
with non-zero status. Perl isn't a shell, so it achieves some things by
direct system calls (e.g. the mkdir(2) system call) rather than by
invoking external executables (/bin/mkdir) like a shell would. That
said, the majority of external commands are going to be subprocesses in
perl -and- the shell (e.g. lynx, rsync, who) so they're the same in that
respect.
So, 'yes', you're right. If you want that specific kind of error
checking you have to ask for it yourself. Look into the 'system'
function built-in to perl.
For example, taking your chdir example, you might try to handle
exceptional conditions... you might say:
# some setup here...
if (-d $somewhere) {
print "Output dir exists\n";
} else {
die "Hey - $somewhere doesn't exist and I don't know what to do\n";
}
or maybe:
unless (-d $somewhere) {
require File:
ath;
File:
ath::mkpath($somewhere, 1, 0775);
}
and then the chdir()
depending on how you think the condition should be handled. Is the
condition exceptional, or is it likely to happen frequently? Do you want
to ask the user for confirmation before altering the filesystem? Can you
check for problems before doing any work (e.g. testing to see if files
exist already)
P