Cover | Table of Contents | Colophon
comp.lang.perl.announce
comp.lang.perl.misc
comp.lang.perl.moderated
comp.lang.perl.modules
http://www.perl.com/CPAN/. The ongoing development and enhancement
of Perl is very much a cooperative effort, and CPAN is the place where
the work of many individuals comes together.
www.perl.com. The multiplexor tries to connect you to a
local, fast machine on a large bandwidth hub. To use the multiplexor,
go to http://www.perl.com/CPAN/; the multiplexor will automatically
route you to a site based on your domain.http://www.perl.com/CPAN (no trailing slash).
When you omit the trailing slash, the CPAN multiplexor
presents a menu of CPAN mirrors from which you select the one you
want. It remembers your choice next time.www.perl.com. The multiplexor tries to connect you to a
local, fast machine on a large bandwidth hub. To use the multiplexor,
go to http://www.perl.com/CPAN/; the multiplexor will automatically
route you to a site based on your domain.http://www.perl.com/CPAN (no trailing slash).
When you omit the trailing slash, the CPAN multiplexor
presents a menu of CPAN mirrors from which you select the one you
want. It remembers your choice next time.ftp.perl.com
ftp.cs.colorado.edu
ftp.cise.ufl.edu
ftp.funet.fi
ftp.cs.ruu.nl
CPAN.html CPAN info page; lists what's available
in CPAN and describes each of the modules
ENDINGS Description of the file extensions, such as .tar, .gz, and .zip
MIRRORED BY A list of sites mirroring CPAN
MIRRORING.FROM A list of sites mirrored by CPAN
README A brief description of what you'll find on CPAN
README.html An HTML-formatted version of the README file
RECENT Recent additions to the CPAN site
RECENT.DAY Recent additions to the CPAN site (daily)
RECENT.html An HTML-formatted list of recent additions
RECENT.WEEK Recent additions to the CPAN site (weekly)
ROADMAP What you'll find on CPAN and where
ROADMAP.html An HTML-formatted version of ROADMAP
SITES An exhaustive list of CPAN sites
SITES.html An HTML-formatted version of SITES
authors A list of CPAN authors
clpa An archive of comp.lang.perl.announce
doc Various Perl documentation, FAQs, etc.
indices All that is indexed.
latest.tar.gz The latest Perl distribution sources
misc Misc Perl stuff like Larry Wall quotes and gifs
modules Modules for Perl version 5
other-archives Other things yet uncategorized
ports Various Perl ports
scripts Various scripts appearing in Perl books
src The Perl sources from various versions
@INC array and prints the names of all modules it finds:find `perl -e 'print "@INC"'` -name '*.pm' -print
by-authors Modules by author's registered CPAN name by-category Modules by subject matter (see below) by-module Modules by namespace (i.e., MIME)
02_Perl_Core_Modules 03_Development_Support 04_Operating_System_Interfaces 05_Networking_Devices_Inter_Process 06_Data_Type_Utilities 07_Database_Interfaces 08_User_Interfaces 09_Interfaces_to_Other_Languages 10_File_Names_Systems_Locking 11_String_Processing_Language_Text_Process 12_Option_Argument_Parameter_Processing 13_Internationalization_and_Locale 14_Authentication_Security_Encryption 15_World_Wide_Web_HTML_HTTP_CGI 16_Server_and_Daemon_Utilities 17_Archiving_and_Compression 18_Images_Pixmap_Bitmap_Manipulation 19_Mail_and_Usenet_News 20_Control_Flow_Utilities 21_File_Handle_Input_Output 22_Microsoft_Windows_Modules 23_Miscellaneous_Modules 99_Not_In_Modulelist
#!/usr/bin/perl
#! (hash-bang or shebang) line
tells the shell to look for the
/usr/bin/perl program and pass the rest of the file
to that program for execution. Sometimes you'll see different
pathnames to the Perl executable, such as /usr/local/bin/perl.
You might see perl5 instead of perl on sites that still depend
on older versions of Perl.
Or you'll see command-line options tacked on the end, such as
the notorious -w switch, which produces warning messages.
But almost all Perl programs
on Unix start with some variation of this line.#! line points to the
location of the Perl interpreter on your system.#! line at the top of
the script, make it executable with chmod +x, and run it.
For 95% of Perl programmers in this world, that's all you'll
care about.
#! line, you can also
specify a short script directly on the command line.
Here are some of the possible ways to run Perl:perl -e 'print "Hello, world\n"' #Unix perl -e "print \"Hello, world\n\"" #Win32
perl testpgm
#! notation, specify
the Perl command on the #! line, make your
script executable, and invoke it from the shell
(as described above).echo "print 'Hello, world'" | perl -
ignoreeof is set):
% perl print "Hello, world\n"; ^D
#! line, it is always examined for
switches as the line is being parsed. Thus,
switches behave consistently regardless of how Perl was invoked.-- switch that terminates switch processing.#!/usr/bin/perl -spi.bak
#!/usr/bin/perl -s -p -i.bak
| Switch | Function |
|---|---|
| -- |
Terminates switch processing, even if the next argument starts with
a minus. It has no other effect.
|
| -0[octnum] |
Specifies the record separator (
$/) as an
octal number. If octnum is not present, the null
character is the separator. Other switches may
precede or follow the octal number. |
| -a |
Turns on autosplit mode when used with -n
or -p. An implicit
split of the @F array is inserted as
the first command inside the implicit while loop
produced by -n or -p.
The default field delimiter is whitespace; a different field delimiter
may be specified using -F. |
| -c |
chdir has no argument.
chdir has no argument and HOME is not set.
> set PATHEXT=%PATHEXT%;.PLXThis setting lets you type:
> myscriptwithout including the file extension. Take care when setting PATHEXT permanently—it also includes executable file types like .com, .exe, .bat, and .cmd. If you inadvertently lose those extensions, you'll have difficulty invoking applications and script files.
use strict pragma in your programs (see
Chapter 8, for more information on pragmas and strict
in particular). This
makes it an error to not explicitly declare your variables.$).
A scalar is either a number, a string, or a reference.
(A reference is a scalar that points to another piece of data. References are
discussed later in this chapter.)
If you provide a string where a number is expected or vice versa, Perl
automatically converts the operand using fairly intuitive rules.@).%).12345 # integer -54321 # negative integer 12345.67 # floating point 6.02E23 # scientific notation 0xffff # hexadecimal 0377 # octal 4_294_967_296 # underline for legibilitySince Perl uses the comma as a list separator, you cannot use a comma for improving legibility of a large number. To improve legibility, Perl allows you to use an underscore character instead. The underscore only works within literal numbers specified in your program, not in strings functioning as numbers or in data read from somewhere else. Similarly, the leading
0x for hex and 0 for octal work only for literals.
The automatic conversion of a string to a number does not recognize these
prefixes—you must do an explicit conversion.{ }.
Compound statements are built out of expressions and blocks.
A conditional expression is evaluated
to determine whether a statement block will be executed.
Compound statements are defined in terms of
blocks, not statements, which means that
braces are required.SOMELABEL here:
SOMELABEL: {
...statements...
}
By convention, labels are all uppercase, so as not to conflict with
reserved words. Labels are used with the loop-control
commands next, last, and redo to alter
the flow of execution
in your programs.if and unless statements execute blocks of code depending
on whether a condition is met. These statements take the following forms:
if (expression) {block} else {block} unless (expression) {block} else {block} if (expression1) {block} elsif (expression2) {block} ... elsif (lastexpression) {block} else {block}
while statement repeatedly executes a block as long as
its conditional expression is true.
For example:
while (<INFILE>) {
print OUTFILE, "$_\n";
}
This loop reads each line from the file opened with the filehandle INFILE
and prints them to the OUTFILE filehandle. The loop will cease when
it encounters an end-of-file.$, @, or
%), such as $_.
The explicit, long-form names shown are the variables' equivalents
when you use the English module by including
"use English;" at the top of your program.$_, which contains
the default input and pattern-searching string.
For example, in the following lines:
foreach ('hickory','dickory','doc') {
print;
}
The first time the loop is executed, "hickory" is printed.
The second time around, "dickory" is printed, and the third time, "doc"
is printed. That's because in each iteration of the loop,
the current string is placed
in $_, and is used by default by print.
Here are the places where Perl will assume $_ even if you don't
specify it:ord and
int, as well as the all file tests (-f, -d) except for
-t, which defaults to STDIN.print and unlink.m//, s///, and tr///
when used without an =~ operator.foreach loop if no other
variable is supplied.grep and map
functions.while test (i.e., <filehandle>).
Note that outside of a while test, this
will not happen.| Associativity | Operators |
|---|---|
| Left | Terms and list operators (leftward) |
| Left |
-> (method call, dereference) |
| Nonassociative |
++ -- (autoincrement, autodecrement) |
| Right |
** (exponentiation) |
| Right |
! ~ \ and unary + and - (logical not, bit-not,
reference, unary plus, unary minus) |
| Left |
=~ !~ (matches, doesn't match) |
| Left |
* / % x (multiply, divide, modulus, string replicate) |
| Left |
+ - . (addition, subtraction, string concatenation) |
| Left |
<< >> (left bit-shift, right bit-shift) |
| Nonassociative |
Named unary operators and file-test operators
|
| Nonassociative |
< > <= >= lt gt le ge (less than, greater than, less
than or equal to, greater than or equal to, and their string
equivalents. |
| Nonassociative |
== != <=> eq ne cmp (equal to, not equal to,
signed comparison, and their string equivalents) |
| Left |
& (bit-and) |
| Left |
| ^ (bit-or, bit-xor) |
| Left |
&& (logical AND) |
| Left |
|| (logical OR) |
| Nonassociative |
.. … |
/
pattern
/.
It matches against the $_ variable by default. If the pattern is found
in the string, the operator returns true ("1"); if there is no
match, a false value ("") is returned.s/
pattern
/
replace
/.
This operator searches $_ by default. If it finds the specified pattern,
it is replaced with the string in replace. If pattern is not
matched, nothing happens.$_ with the =~ binding operator (or the negated !~
binding operator, which returns true if the pattern is not matched).
For example:
$text =~ /sampo/;
m/
pattern
/gimosx
| Modifier | Meaning |
|---|---|
g
|
sub name {block} sub name (proto) {block}Prototypes allow you to put constraints on the arguments you provide to your subroutines.
$subref = sub {block};
&) is the identifier used to
call subroutines.
Most of the
time, however, subroutines can be used in an
expression just like built-in functions.
To call subroutines directly:
name(args); # & is optional with parentheses
name
args; # Parens optional if predeclared/imported
&name; # Passes current @_ to subroutine
To call subroutines indirectly (by name or by reference):
&$subref(args); # & is not optional on indirect call
&$subref; # Passes current @_ to subroutine
@_.return statement to return
a value and leave the subroutine
at any point.$a = "fondue";
@alist = ("pitt", "hanks", "cage", "cruise");
%song = ("mother" => "crying", "brother" => "dying");
sub freaky_friday { s/mother/daughter/ }
# Create references
$ra = \$a;
$ralist = \@alist;
$rsong = \%song;
$rsub = \&freaky_friday; # '&' required for subroutine names
References to scalar constants are created similarly:
$pi = \3.14159; $myname = \"Charlie";Note that all references are prefixed by a
$, even
if they refer to an array or hash. All references are scalars, thus you
can copy a reference to another scalar or even reference
another reference:
$aref = \@names; $bref = $aref; # both refer to @names $cref = \$aref; # $cref is a reference to $arefBecause arrays and hashes are collections of scalars, you can create references to individual elements by prefixing their names with backslashes:
$star = \$alist[2]; # refers to third element of @alist
$action = \$song{mother}; # refers to the 'mother' value of %song