how to do sh's -e in perl?

D

Dan Jacobson

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?
 
S

Steve Grazzini

Dan Jacobson said:
I can get the shell's "set -u" protection in perl with
"use strict", but what about the shells' "set -e"?

% perldoc -f -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?

You can; quit whinging.

% perldoc Fatal

There are also 'FATAL' warnings, which might catch a few
of the unwrappable functions (like system() and print()).

% perldoc warnings
% perldoc perllexwarn
 
P

pkent

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::path;
File::path::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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,262
Latest member
EffiePju4

Latest Threads

Top