Marilyn Sander wrote:
On Nov 9, 2005, at 4:52 PM, Pablo Wolter wrote:
The system function call returns a boolean value as return value, I don't
remember if 1 is for fail and 0 for success or viceversa.
Actually it is not a boolean value. It is a two-byte value, and each
byte is an integer. You need to look up the system() function in
some Perl documentation. If you get two bytes of zero back, that
means system() and the command it invoked ran successfully. If you
get a non-zero value, the lower byte indicates what error was found
in the Perl run-time; the upper byte is the return code from the
command executed by system().
Actually, it's not a boolean nor a two-byte value. To describe it in C
terms: it is a union of an integer and a packed struct; the struct is an
integer, a bit, and an unsigned char.
How to Use system():
--------------------
First decide if you want to use your shell as an interpreter. To use
your shell, call system() with a single string containing the command.
system( 'ls *' ); # system( 'dir *' ); for MS DOS
All the rules about meta-characters and quoting in the shell apply. If
you don't want to use your shell, call system() with a list. (This is
recommended for cross-platform scripts since you don't know what shell
the user may be running.)
system( 'ls', '*' ); # system( 'dir', '*' );
See `perldoc -f system` and `perldoc -f exec` for details.
Determining If system() Worked:
-------------------------------
The system() function returns a value that is also contain in the
special variable $? or $CHILD_ERROR if you had `use English;`. First,
test it to see if the program could be started.
system( 'Some_string_that_is_not_a_real_program' );
die "cannot start system: $!" if $? == -1;
Note that the special variable $! (or $OS_ERROR) contains an English
string describing the error.
Now, break up the structure of the value.
my $exit_status = ( $? >> 8 );
my $created_core_file = ( 0x80 & $? );
my $terminating_signal = ( 0x7f & $? );
The $exit_status is the exit value of the program. See `perldoc -f exit`
and `man 3 exit`.
The $created_core_file is true if the program created a core file. Core
files contain the program and the data at the time of termination.
Special programs like debuggers can be used to view this file. If your
program contains sensitive data, it is important to remove this file as
soon as possible. Leaving core files lying around is a security risk. MS
DOS almost never leaves a core file.
The $terminating_signal is the signal that caused the program to stop.
Normal termination is zero. Other signal can be found in `man signal`.
If this is non-zero, the program was interrupted before its task was
completed. You may have temporary files or partial results hanging
around. MS DOS does not have signals, so this is always zero.
Using system(), Simplified:
---------------------------
The simplest way to use the system() function is just to test the
returned value for zero. If it is, the call worked.
system( ... ) == 0 or die "system call failed";
--
Just my 0.00000002 million dollars worth,
--- Shawn
"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>