Execute System Command and Shell from Perl

Revision as of 09:20, 13 April 2008 by Admin (Talk | contribs)

The commands open, exec, and backticks, system uses the shell to start the program whenever it's called with one argument.

system

Passes a string along to the underlying system.

Executes a system command and returns a value that is the exit status of the program as returned by the wait() call. Like exec(), system() allows you to lie to a program about its name if you use the ``system PROGRAM LIST syntax. Because system() and backticks block SIGINT and SIGQUIT, killing the program they're running doesn't actually interrupt your program.

 @args = ("command", "arg1", "arg2");
 system(@args) == 0
   or die "system @args failed: $?"

exec

Passes a string along to the underlying system.

The exec function executes a system command without return.

open

backtick

The "backtick" is the ` character (ASCII code 96, backwards-slanted single quote, or a grave accent without the character). By quoting a command string with backticks the command is processed by the system and whatever is on stdout ends up where the quoted expression is within the Perl program.

Using backtick is not exactly the same as using system(). Backtick and open will gather the program's STDOUT, while system will not.

This works with any commands that can be run from the shell, or in Perl it can be used as the argument to a function or part of an expression. It works in concert with pipes, as pipes redirect stdout to stdin, whereas backticks convert stdout to command run-time arguments.


 

 

Last modified on 13 April 2008, at 09:20