The system() Function

The simplest way to run a command outside Perl is to use the system function. The system function pauses the Perl program, runs the external command, and then continues running your Perl program. The syntax for system is

system command;

where command is the command that you want run. The return value from system is 0 if everything goes okay or nonzero if a problem occurs. Notice that this result is backward from the normal Perl values of true and false.

The following is an example of system under Unix:

system("ls -lF");        # Print a file listing
# print system's documentation
if ( system("perldoc -f system") ) {
    print "Your documentation isn't installed correctly!
";
}

This next example shows system under MS-DOS/Windows:

system("dir /w");        # Print a file listing
# print system's documentation
if ( system("perldoc -f system") ) {
    print "Your documentation isn't installed correctly!
";
}

For the most part, system works the same under both architectures. The point to remember is that the commands are fundamentally different from one OS to another. To get a file listing in MS-DOS, you use the dir command; to get a file listing under Unix, you use the ls command. In very rare instances—perldoc, for example—the commands are similar under Unix and MS-DOS.

When the system function is running the external command, the output of that command is displayed on your screen as though your Perl program were printing it. If that external command needs input, the input comes from your terminal the same way your Perl programs read input from the terminal. The command run by system inherits the STDIN and STDOUT file descriptors, so the external command does I/O from the exact same place as your Perl program. Invoking fully interactive programs from system is possible.

Consider the following example under Unix:

$file="myfile.txt";
system("vi $file");

Now consider this example under Windows/MS-DOS:

$file="myfile.txt";
system("edit $file");

Each of the preceding examples runs an editor on myfile.txt—vi for Unix and edit for DOS. The editor runs full screen, of course, and all the normal editor commands work. When the editor exits, control returns to Perl.

You can use the system function to run any program, not just console-mode programs (text). Under Unix, this example runs a graphical clock:

system("xclock -update 1");

Under MS-DOS/Windows, the following example invokes a graphical file editor:

system("notepad.exe myfile.txt");

The Underlying Command Interpreter

The system function (and actually most of the functions in this hour) allows you to use any of your command interpreter's features that would be available to you at the command prompt. You can do so because the Perl system command invokes a copy of a shell (/bin/sh on Unix, command.com in MS-DOS/Windows) and then gives your system command to that shell. As a result, you can perform redirection (>), piping (|), and background tasks under Unix (&), as well as use other features your command interpreter may offer.

For example, to run an external command and capture its output in a file, you use the following command:

system("perldoc perlfaq5 > faqfile.txt"); 

The preceding command runs perldoc perlfaq5 and captures its output in a file called faqfile.txt. This particular syntax works on both DOS and Unix.

Some features, such as pipes and background tasks, work as you would expect under the Unix operating system, as you can see here:

# Sort the file whose name is in $f and print it
system("sort $f | lp");  # Some systems use "lpr"

# Run "xterm" and immediately return
system("xterm &");

In the last example, the xterm program is started, but the & causes the Unix shell to start the process in the background. This means that, although the process continues to run, the system function finishes and returns control to Perl. Perl does not wait for xterm to finish.

By the Way

Under Unix, Perl always uses /bin/sh—or a reasonable facsimile—for the system function, pipes, and backticks (discussed later in this hour). It does so regardless of what your personal shell is configured to be. This use provides some measure of portability across Unix systems.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.255.5