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
';
} else {
print 'system cp failed $rc
';
}
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.
Modules and CPAN
A great strength of the Perl community (and the Linux community) is that it is an open source community. This community support is expressed for Perl via CPAN, which is a network of mirrors of a repository of Perl code.
Most of CPAN is made up of
Perl comes with a set of standard modules installed. Those modules should contain much of the functionality that you will initially need with Perl. If you need to use a module not installed with Fedora, use the CPAN module (which is one of the standard modules) to download and install other modules onto your system. At http://www.perl.com/CPAN, you will find the CPAN Multiplex Dispatcher, which attempts to direct you to the CPAN site closest to you.
Typing the following command puts you into an interactive shell that gives you access to CPAN. You can type help
at the prompt to get more information on how to use the CPAN program.
$ perl -MCPAN -e shell
After you have installed a module from CPAN (or written one of your own), you can load that module into memory, where you can use it with the use
function:
use Time::CTime;
use
looks in the directories listed in the variable @INC
for the module. In this example, use
looks for a directory called Time
, which contains a file called CTime.pm
, which in turn is assumed to contain a package called Time::CTime
. The distribution of each module should contain documentation on using that module.
For a list of all the standard Perl modules (those that come with Perl when you install it), see perlmodlib
in the Perl documentation. You can read this document by typing perldoc perlmodlib
at the command prompt.
You will use these commands and tools when using Perl with Linux:
> a2p
— A filter used to translate awk
scripts into Perl
> find2perl
— A utility used to create Perl code from command lines, using the find command
> pcregrep
— A utility used to search data, using Perl-compatible regular expressions
> perlcc
— A compiler for Perl programs
> perldoc
— A Perl utility used to read Perl documentation
> s2p
— A filter used to translate sed
scripts into Perl
> vi
— The vi
(actually vim
) text editor
Reference
> http://www.perl.com/ — Tom Christiansen maintains the Perl language home page. This is the place to find all sorts of information about Perl, from its history and culture to helpful tips. This is also the place to download the Perl interpreter for your system.
> http://www.perl.com/CPAN — This is part of the site just mentioned, but it merits its own mention. CPAN (Comprehensive Perl Archive Network) is the place for you to find modules and programs in Perl. If you write something in Perl that you think is particularly useful, you can make it available to the Perl community here.
> http://www.perl.eom/pub/q/FAQs — Frequently Asked Questions index of common Perl queries; this site offers a handy way to quickly search for answers about Perl.
> http://learn.perl.org/ — One of the best places to start learning Perl online. If you master Perl, go to http://jobs.perl.org.
> http://www.pm.org/ — The Perl Mongers are local Perl users groups. There might be one in your area. The Perl advocacy site ishttp://www.perl.org/.
> http://www.tpj.com/ —
> http://www-106.ibm.com/developerworks/linux/library/l-p101 — A short tutorial about one-line Perl scripts and code.
Books