home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Perl CookbookPerl CookbookSearch this book

16.3. Replacing the Current Program with a Different One

16.3.3. Discussion

The exec function in Perl is a direct interface to the execlp(2) syscall, which replaces the current program with another, leaving the process intact. The program that calls exec gets wiped clean, and its place in the operating system's process table is taken by the program specified in the arguments to exec. As a result, the new program has the same process ID ($$) as the original program. If the specified program couldn't be run, exec returns a false value and the original program continues. Be sure to check for this.

As with system (see Recipe 16.2), an indirect object identifies the program to be run:

exec { '/usr/local/bin/lwp-request' } 'HEAD', $url;

The first real argument ('HEAD' here) is what the new program will be told it is. Some programs use this to control their behavior, and others use it for logging. The main use of this, however, is that exec called with an indirect object will never use the shell to run the program.

If you exec yourself into a different program, neither your END blocks nor any object destructors will be automatically run as they would if your process actually exited.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.