Closed p5pRT closed 20 years ago
Is $! supposed to be set after system?
From p. 230 of the Camel (2nd edition) one might gather this and a lot of people seem to believe this to be the case.
But I am thinking it's a mis-assumption and system() (unlike\, say\, open) does not set $!.
This should be _explicitly_ documented\, especially in light of the example on p. 230 of Programming Perl which solidly implies that $! may get set.
However\, one of the clp-mod members did some playing with a C program that indicated errno is being set after a program is run\, so perhaps system() _should_ be setting $! and isn't.
Is this a bug?
(It's either a bug in the book\, i.e. Programming Perl should document that the use of $! in the code on p. 230 is not _directly_ related to system()... unlike certain other function such as open()\, system() does not actually set $!.
_Or_ system() is supposed to set $! and I can't make it do so.
If it's a documentation bug only I'll follow up with that. If it is a bug in system() you folks get it.
Vicki Brown \vlb@​cfcl\.com writes:
From the Camel\, p. 230 ) ) $rc = 0xffff & system(@args); [...] ) elsif ($rc == 0xff00) { ) print "command failed: $!\n";
I cannot make $! contain anything useful no matter how hard I try\, or anyone else tries
At 21:06 -0600 07/07/1999\, Tom Christiansen wrote in comp.lang.perl.moderated:
:If $! is meaningful in any situation after running system(...)
It was when I wrote that.
:I'd appreciate an example. $! is empty when I try : system("cat food"); # no such file food; exit(2) from cat : system("cats food"); # no such program cats : system("command that seg faults");
Testing...
bsd% perl -le 'system("nonesuch"); print $!' \
linux% perl -le 'system("nonesuch"); print $!' \
sunos% perl -le 'system("nonesuch"); print $!' No such file or directory
and
bsd% perl -le 'system("/"); print $!' \
linux% perl -le 'system("/"); print $!' \
sunos% perl -le 'system("/"); print $!' Permission denied
Well\, something's odd. I'd report it.
One of the other folks on the list quoted the docs thusly:
C\
returns C\<-1> if it is unable to request that the command be run. This happens if C\<fork()> fails\, for example. C\<$!> will say why C\<fork()> or some other call failed [though\, on most implementations\, I think C\<fork()> is the only thing that can fail to cause this case]. This isn't explicitly documented with Perl\, possibly because it depends on your operating system.
So perhaps $! is not meant to reflect the result of system()and the book (Programming Perl) should mention this as a comment in the code on p. 230.
However\, another clp-mod member tried this:
At 19:00 -0700 07/07/1999\, Dirk Myers wrote:
If you take the programs that follow (test.c is compiled to test -- gcc test.c -o test -- \, and the file "gabba" in the current directory exists) you get:
harpo 17% ./test.pl The error was -1\, errno was 17\, strerror was File exists 17 17 File exists
... it *looks like* $! works\, but isn't actually getting set to errno.
Is this a bug? I dunno... the C program is clearly setting errno and exiting with an error\, but $! sure isn't getting it...
--- snip here for test.c --- /* Test program to blow up with an error * ... you gotta touch 'gabba' in the current directory before * running it... */
#include \<stdlib.h> #include \<errno.h> #include \<fcntl.h> #include \<stdio.h>
void main(void) { int rv ; rv = open("./gabba"\, O_EXCL | O_CREAT | O_SHLOCK | O_EXLOCK) ; if (rv != 0 ) { printf ("The error was %d\, errno was %d\, strerror was %s\n"\, rv\, errno\, strerror(errno)) ; } exit (errno) ; --- snip for test.c ---
--- snip for test.pl --- #!/usr/local/bin/perl
$rv = system "./test" ; if ($rv) { $rv = $rv >> 8 ; print "$rv $!\n" ; $! = $rv ; print "$rv $!\n" ; } else { print "nope.\n" ; } --- snip for test.pl ---
Migrated from rt.perl.org#977 (status was 'resolved')
Searchable as RT977$