rhl6u312

Lemur zaprasza

Red Hat® Linux 6 Unleashed










Chapter 31: Perl Programming





Previous
ChapterNext
Chapter










Sections in this Chapter:












A Simple Perl Program





Regular
Expressions











Perl Variables
and Data Structures





Access
to the Shell





For More
Information






Conditional
Statements: if/else and unless




















Modules
and CPAN








 

Previous
SectionNext
Section





Chapter 31

Perl Programming

by Rich Bowen


Perl (Practical Extraction and Report Language) was
developed in the mid-1980s by Larry Wall, who was already
responsible for a number of rather important UNIX utilities. Larry claims that
Perl really stands for "Pathologically Eclectic Rubbish Lister." With
the birth of the WWW in the early '90s, Perl took off as the language of
choice for CGI programming. With the recent burst of interest in the Open Source
movement, Perl has gotten almost as much press as Linux.


Perl, according to Larry, is all about "making easy things
easy, and hard things possible." So many programming languages make you
spend an undue amount of time doing stuff to keep the language happy before
you ever get around to making it do what you want. Perl lets you get your work
done without worrying about things like memory allocation and variable typing.


Perl contains the best features of C, Basic, and a variety of
other programming languages, with a hearty dollop of awk, sed, and shell scripting
thrown in. In Perl, "There is more than one way to do it." This is
the unofficial motto of Perl, and comes up so often that it is usually abbreviated
as TIMTOWTDI. If you are familiar with some other programming language, chances
are you can write functional Perl code. A version of Perl comes with Red Hat,
but it is typically several versions out of date. As of this writing, the current
version of Perl is 5.005_03 (that's Perl version 5 point 5, patch level
3). You can determine what version of Perl you have installed by typing perl
-v
at a shell prompt.


Perl is an interpreted language. The interpreter has been ported
to every operating system that you've heard of, and several you haven't.
For UNIX and UNIX-like operating systems, you can just download the code from

and build it yourself.


A Simple Perl Program
To introduce
you
to the absolute basics of Perl programming, Listing 31.1 illustrates a trivial
Perl program.



LISTING 31.1  A Trivial Perl Program

#!/usr/bin/perl
print "Look at all the camels!\n";

That's the whole thing. Type that in, save it to a file
called trivial.pl,
chmod +x it, and execute
it.
The #! line is technically not part
of the Perl code at all ( the # character is the
comment character in Perl), but is instead a message to the shell, telling it
where it should go for the executable to run this program. That is standard
practice in shell programming, as is discussed in Chapter
25, "Shell Programming." If, for some reason, Perl is not located
at /usr/bin/perl on your system, you can locate the correct location of Perl
by using the which command.


Note -
#! is often pronounced "she-bang,"
which is short for "sharp," (the musical name for the
# character) and "bang," which is another
name for the exclamation point.

which perl
If you do not have Perl installed, you might want to skip to "For
More Information" in this chapter to find out where you can obtain
the Perl interpreter. However, a version of Perl comes with Red Hat, so this
should not be the case.
The second line does
precisely what you would expect--it prints the text enclosed in quotation marks.
\n is the escape sequence for a newline
character.Perl
statements are terminated with a semicolon. A Perl statement can extend over
several actual screen lines. Alternatively, you can have Perl statements in one
line. Perl is not particularly concerned about
whitespace.The #
character indicates that the rest of the screen line is a comment. That is,
there is a comment from the # character until the
next newline and it is ignored by the interpreter. Exceptions to this include
when the # character is in a quoted string and when
it is being used as the delimiter in a regular
expression.A block of code, such as what might appear
inside a loop or a branch of a conditional statement, is indicated with curly
braces
({}).
Included with the Perl installation is a document called perlfunc,
which lists all of the available Perl functions and their usage. You can view
this document by typing perldoc perlfunc
at the command line. You can also find this document online at .





Red Hat® Linux 6 Unleashed










Chapter 31: Perl Programming





Previous
ChapterNext
Chapter










Sections in this Chapter:












A Simple Perl Program





Regular
Expressions











Perl Variables
and Data Structures





Access
to the Shell





For More
Information






Conditional
Statements: if/else and unless




















Modules
and CPAN








 

Previous
SectionNext
Section





© Copyright Macmillan USA. All rights reserved.

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • teen-mushing.xlx.pl
  • Wątki
    Powered by wordpress | Theme: simpletex | © Lemur zaprasza