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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ SYSTEM INTERACTION 85When executed, the variable $uptime would contain the output from the uptime command:21:20:07 up 202 days, 5:52, 3 users, load average: 0.01, 0.02, 0.00Obviously, the output from your uptime command probably will be different.System Processes As FilehandlesYou learned about filehandles earlier in this chapter. You can also use system processes as filehandlesby using the same open() function as you use <strong>to</strong> create a filehandle. Like file-flavoredfilehandles, you can open process filehandles for reading and writing.The syntax of the open() function is largely the same for processes as it is for files, exceptthat the pipe character (|) is used instead of the greater-than/less-than characters. The locationof the pipe character determines whether the process handle is opened for reading or writing.Creating a process handle for reading looks like this:open(UPTIME, "uptime|");Creating a handle for writing looks like this:open(PRINTER, "|lpr ");Then you could print <strong>to</strong> that handle, similar <strong>to</strong> how you print <strong>to</strong> a filehandle:print PRINTER "Printing something from this handle\n";Using System Processes <strong>With</strong>in a CGI ProgramUsing a system call from within a <strong>Perl</strong>-based CGI script is no different from using the samesystem call from within another <strong>Perl</strong> program. The one exception is that you must pay particularattention <strong>to</strong> the environment within which the script will be run.Many times, the environment will be that of the web server user. This means that the <strong>Perl</strong>program might not have access <strong>to</strong> the same commands, files, and other environmental bits asyour user on the same system. A common symp<strong>to</strong>m is that the script will appear <strong>to</strong> run finewhen you execute it from your shell, but when you attempt <strong>to</strong> access the script through theweb browser, it won’t work, possibly giving the infamous Internal Server Error message that<strong>Perl</strong> programmers everywhere have come <strong>to</strong> love.Some web servers use Apache’s suEXEC feature for the execution of CGI scripts. <strong>With</strong>suEXEC, CGI scripts are executed as the user who owns the program, which is frequently thesame as your userid for many installations.Security Considerations with System InteractionWhen working with the system—files or processes—from within a <strong>Perl</strong> program, you must payadditional attention <strong>to</strong> your surroundings. It’s quite possible and unfortunately easy <strong>to</strong> overwriteexisting and essential files by opening a file for writing instead of appending.Whenever you work with anything outside your <strong>Perl</strong> program, there is always a risk of introducingunknown data in<strong>to</strong> your <strong>Perl</strong> program or doing something unintentional <strong>to</strong> the systemitself. The former is an important concern for developers; the latter is primarily a concern for thesystem administra<strong>to</strong>r. For those of us who frequently wear both hats, it’s important <strong>to</strong> perform

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

Saved successfully!

Ooh no, something went wrong!