Книга: Fedora™ Unleashed, 2008 edition
Access to the Shell
Разделы на этой странице:
Access to the Shell
Perl can perform any process you might ordinarily perform if you type commands to the shell through the ``
syntax. For example, the code in Listing 25.4 prints a directory listing.
LISTING 25.4 Using Backticks to Access the Shell
$curr_dir = `pwd`;
@listing = `ls -al`;
print "Listing for $curr_dirn";
foreach $file (@listing) {
print "$file";
}
NOTE
The ``
notation uses the backtick found above the Tab key (on most keyboards), not the single quotation mark.
You can also use the Shell
module to access the shell. Shell
is one of the standard modules that comes with Perl; it allows creation and use of a shell-like command line. Look at the following code for an example:
use Shell qw(cp);
cp ("/home/httpd/logs/access.log", "/tmp/httpd.log");
This code almost looks as if it is importing the command-line functions directly into Perl. Although that is not really happening, you can pretend that the code is similar to a command line and use this approach in your Perl programs.
A third method of accessing the shell is via the system
function call:
$rc = 0xffff & system('cp /home/httpd/logs/access.log /tmp/httpd.log');
if ($rc == 0) {
print "system cp succeeded n";
} else {
print "system cp failed $rcn";
}
The call can also be used with the or die
clause:
system('cp /home/httpd/logs/access.log /tmp/httpd.log') == 0
or die "system cp failed: $?"
However, you can't capture the output of a command executed through the system
function.
- Managing Files with the Shell
- Access Variable Data Files in the
- The FTP User
- Table B-6. Interfaces to operating system utilities
- Практическая работа 53. Запуск Access. Работа с объектами базы данных
- 4.4.4 The Dispatcher
- About the author
- Chapter 7. The state machine
- Appendix E. Other resources and links
- Example NAT machine in theory
- The final stage of our NAT machine
- Compiling the user-land applications