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!\n";
}

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

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.