Erm, backticks don't set $!; they set $?.
$ perl -le '`bad_command`;print $?;`echo`;print $?'
-1
0
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] |
Checking $! for errors is never a good idea unless you're sure the last syscall produced an error. $! may have any kind of value if the last syscall did not produce an error or is not documented to set $!.
As far as I can see the only way to check for this case is to see if the `` operator returned undef:
my $hostname = `hostname`;
unless (defined $hostname) {
print "found error of: $!\n";
}
update: as Fletch mentioned, `` does not set $! but $? so this code doesn't really work.
In addendum: be aware that not all functions that actually set $! are documented as doing so in the man pages. The only source that I know that seems to mention all of them is the Camel book.
| [reply] [d/l] |
Your answer was better before the update. qx() can set $! if there was a failure in the parent process. But qx() does always set $? and you need to look at $? in order to figure out if $! was also set. Despite not finding this in the documentation at the moment, I believe that $? being -1 means that $! should be checked / reported.
| [reply] |
If I'm doing quick and dirty scrips then I just shell using backticks and don't worry much about the commands not running. If I start to worry about errors then it's time to find a way to do it using perl calls. For your sample in this instance Sys::Hostname is available as a core module and has fallback and error reporting.
<slings-and-arrrows-off>Actually, as a confession, if the problem is going to need a large proportion of shell builtins then I just code in Bash.<slings-and-arrows-on>
<\p>
s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s
|-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,,
$|=1,select$,,$,,$,,1e-1;print;redo}
| [reply] [d/l] |