Operating System Interaction



next up previous
Next: File Handling Up: Previous: C Library Routines

Operating System Interaction

Perl can execute system commands in several ways.

Perl can run shell commands via the system routine. This acts essentially like C's system(3) call. A string is passed to the shell for execution. The output from the command is sent to standard output. The exit status is put into the $? variable (actually, the entire status word is put into $?. Read the man page for details).

Perl also evaluates backquotes (also known as ``backticks'' or ``grave accents'') in way similar to the shell. This is useful when you want to run a shell command and capture the output. Here is an example in which a script gets the name of the host:

$host = `hostname`;
chop($host);

Again, the exit status of this command will be put into $?. Note that we need to chop off the newline from the output.