10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

84CHAPTER 4 ■ SYSTEM INTERACTIONsection won’t be a rehash of every bit of information about such an undertaking. Rather, thissection will provide some refresher material <strong>to</strong> ensure we’re all talking the same language.Executing System Processes from a <strong>Perl</strong> ProgramWhen <strong>Perl</strong> runs a system process, it inherits the traits of its parent. Recall from the earlier sectionon filehandles that a <strong>Perl</strong> program inherits the three standard filehandles: STDIN, STDOUT, andSTDERR. The <strong>Perl</strong> program also inherits other things, like the uid of the parent, the umask, thecurrent direc<strong>to</strong>ry, and other such environmental variables. <strong>Perl</strong> provides the %ENV hash as means<strong>to</strong> access and change the environment variables that are inherited by your <strong>Perl</strong> program. You caniterate through this hash in the same way that you would any other hash <strong>to</strong> see the environmentvariables.foreach $key (keys %ENV) {print "Environment key $key is $ENV{$key}\n";}The fork() and exec() methods of firing a system command are the most flexible method ofworking with system commands available in <strong>Perl</strong>. Unfortunately, they’re also the most complexand arguably the least used, especially when it comes <strong>to</strong> CGI programming. I’m not going <strong>to</strong> clutterthese pages with a discussion of fork() and exec(), but rather refer you <strong>to</strong> the perlfuncdocument pages for more information about fork(), exec(), kill(), wait(), and waitpid().Here, we’ll look at using the system() function, run quotes, and system processes as filehandles.The system FunctionThe system() function is a common way <strong>to</strong> fire off a new process from a <strong>Perl</strong> program. Whenyou use the system() function, a new process is created or handed off <strong>to</strong> /bin/sh, and the <strong>Perl</strong>program waits until the process is finished running.The exit status from the system() function is passed back <strong>to</strong> the <strong>Perl</strong> program. It’s important<strong>to</strong> note that this exit status is not the exit status of the command that the system() functionactually runs, but the exit status of the shell in which the command is run. This is an importantdistinction because it means that you can’t rely on the exit status of the system() function as anindication of whether or not the actual command run by the function completed successfully.Here’s an example of the system() function in action:system("uptime");Run QuotesThe next method <strong>to</strong> execute a system process from within a <strong>Perl</strong> program goes by a fewnames—backquotes, backticks, or run quotes. I’ll be using the name run quotes for no reasonother than that’s the name that I’ve heard used the most often.Run quotes are different from the system() function in that they return the output of thecommand. If you want <strong>to</strong> capture the output of the command, using run quotes provides aneasy way <strong>to</strong> accomplish the task, as this example shows:$uptime = `uptime`;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!