home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Il CD di internet
/
CD.iso
/
SOURCE
/
D
/
PERL
/
PERL-4.036
/
PERL-4
/
perl-4.036
/
Info
/
perl.info-5
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
Macintosh to JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
GNU Info File
|
1994-07-08
|
47.2 KB
|
1,217 lines
This is Info file perl.info, produced by Makeinfo-1.55 from the input
file perltexi.
This file documents perl, Practical Extraction and Report Language,
and was originally based on Larry Wall's unix-style man page for perl.
GNU Texinfo version adapted by Jeff Kellem
<composer@Beyond.Dreams.ORG>.
Copyright (C) 1989, 1990, 1991, 1992, 1993 Larry Wall Texinfo
version Copyright (C) 1990, 1991, 1993 Jeff Kellem
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled "GNU General Public License" and "Conditions
for Using Perl" are included exactly as in the original, and provided
that the entire resulting derived work is distributed under the terms
of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the section entitled "GNU General Public License"
and this permission notice may be included in translations approved by
the Free Software Foundation instead of in the original English.
File: perl.info, Node: Miscellaneous Functions, Prev: Variable Functions, Up: Commands
Miscellaneous Functions
=======================
dump LABEL
dump
This causes an immediate core dump. Primarily this is so that you
can use the `undump' program to turn your core dump into an
executable binary after having initialized all your variables at
the beginning of the program. When the new binary is executed it
will begin by executing a `goto LABEL' (with all the restrictions
that `goto' suffers). Think of it as a `goto' with an intervening
core dump and reincarnation. If LABEL is omitted, restarts the
program from the top. *WARNING*: any files opened at the time of
the dump will *NOT* be open any more when the program is
reincarnated, with possible resulting confusion on the part of
perl. See also `-u'.
Example:
#!/usr/bin/perl
require 'getopt.pl';
require 'stat.pl';
%days = (
'Sun',1,
'Mon',2,
'Tue',3,
'Wed',4,
'Thu',5,
'Fri',6,
'Sat',7);
dump QUICKSTART if $ARGV[0] eq '-d';
QUICKSTART:
do Getopt('f');
...
eval(EXPR)
eval EXPR
eval BLOCK
eval
EXPR is parsed and executed as if it were a little *perl* program.
It is executed in the context of the current *perl* program, so
that any variable settings, subroutine or format definitions
remain afterwards. The value returned is the value of the last
expression evaluated, just as with subroutines. If there is a
syntax error or runtime error, or a `die' statement is executed, an
undefined value is returned by `eval', and `$@' is set to the
error message. If there was no error, `$@' is guaranteed to be a
null string. If EXPR is omitted, evaluates `$_'. The final
semicolon, if any, may be omitted from the expression.
Note that, since `eval' traps otherwise-fatal errors, it is useful
for determining whether a particular feature (such as `dbmopen' or
`symlink') is implemented. If is also *Perl*'s exception trapping
mechanism, where the `die' operator is used to raise exceptions.
If the code to be executed doesn't vary, you may use the `eval
BLOCK' form to trap run-time errors without incurring the penalty
of recompiling each time. The error, if any, is still returned in
`$@'. Evaluating a single-quoted string (as EXPR) has the same
effect, except that the `eval EXPR' form reports syntax errors at
run time via `$@', whereas the `eval BLOCK' form reports syntax
errors at compile time. The `eval EXPR' form is optimized to
`eval BLOCK' the first time it succeeds. (Since the replacement
side of a substitution is considered a single-quoted string when
you use the `e' modifier, the same optimization occurs there.)
Examples:
# make divide-by-zero non-fatal
eval { $answer = $a / $b; }; warn $@ if $@;
# optimized to same thing after first use
eval '$answer = $a / $b'; warn $@ if $@;
# a compile-time error
eval { $answer = };
# a run-time error
eval '$answer ='; # sets $@
ord(EXPR)
ord EXPR
ord
Returns the numeric ascii value of the first character of EXPR.
If EXPR is omitted, uses `$_'.
q/STRING/
qq/STRING/
qx/STRING/
These are not really functions, but simply syntactic sugar to let
you avoid putting too many backslashes into quoted strings. The
`q' operator is a generalized single quote, and the `qq' operator a
generalized double quote. The `qx' operator is a generalized
backquote. Any non-alphanumeric delimiter can be used in place of
`/', including newline. If the delimiter is an opening bracket or
parenthesis, the final delimiter will be the corresponding closing
bracket or parenthesis. (Embedded occurrences of the closing
bracket need to be backslashed as usual.) Examples:
$foo = q!I said, "You said, 'She said it.'"!;
$bar = q('This is it.');
$today = qx{ date };
$_ .= qq
*** The previous line contains the naughty word "$&".\n
if /(ibm|apple|awk)/; # :-)
rand(EXPR)
rand EXPR
rand
Returns a random fractional number between 0 and the value of
EXPR. (EXPR should be positive.) If EXPR is omitted, returns a
value between 0 and 1. See also `srand()'.
srand(EXPR)
srand EXPR
srand
Sets the random number seed for the `rand' operator. If EXPR is
omitted, does `srand(time)'.
sprintf(FORMAT,LIST)
Returns a string formatted by the usual `printf' conventions. The
`*' character is not supported.
vec(EXPR,OFFSET,BITS)
Treats a string as a vector of unsigned integers, and returns the
value of the bitfield specified. May also be assigned to. BITS
must be a power of two from 1 to 32.
Vectors created with `vec()' can also be manipulated with the
logical operators `|', `&' and `^', which will assume a bit vector
operation is desired when both operands are strings. This
interpretation is not enabled unless there is at least one `vec()'
in your program, to protect older programs.
To transform a bit vector into a string or array of 0's and 1's,
use these:
$bits = unpack("b*", $vector);
@bits = split(//, unpack("b*", $vector));
If you know the exact length in bits, it can be used in place of
the *.
File: perl.info, Node: Precedence, Next: Subroutines, Prev: Commands, Up: Top
Precedence
**********
*Perl* operators have the following associativity and precedence:
nonassoc print printf exec system sort reverse
chmod chown kill unlink utime die return
left ,
right = += \-= *= etc.
right ?:
nonassoc ..
left ||
left &&
left | ^
left &
nonassoc == != <=> eq ne cmp
nonassoc < > <= >= lt gt le ge
nonassoc chdir exit eval reset sleep rand umask
nonassoc -r -w -x etc.
left << >>
left + - .
left * / % x
left =~ !~
right ! ~ and unary minus
right **
nonassoc ++ --
left `('
As mentioned earlier, if any list operator (`print', etc.) or any
unary operator (`chdir', etc.) is followed by a left parenthesis as
the next token on the same line, the operator and arguments within
parentheses are taken to be of highest precedence, just like a normal
function call. Examples:
chdir $foo || die; # (chdir $foo) || die
chdir($foo) || die; # (chdir $foo) || die
chdir ($foo) || die; # (chdir $foo) || die
chdir +($foo) || die; # (chdir $foo) || die
but, because `*' is higher precedence than `||':
chdir $foo * 20; # chdir ($foo * 20)
chdir($foo) * 20; # (chdir $foo) * 20
chdir ($foo) * 20; # (chdir $foo) * 20
chdir +($foo) * 20; # chdir ($foo * 20)
rand 10 * 20; # rand (10 * 20)
rand(10) * 20; # (rand 10) * 20
rand (10) * 20; # (rand 10) * 20
rand +(10) * 20; # rand (10 * 20)
In the absence of parentheses, the precedence of list operators such
as `print', `sort' or `chmod' is either very high or very low depending
on whether you look at the left side of operator or the right side of
it. For example, in
@ary = (1, 3, sort 4, 2);
print @ary; # prints 1324
the commas on the right of the sort are evaluated before the sort,
but the commas on the left are evaluated after. In other words, list
operators tend to gobble up all the arguments that follow them, and then
act like a simple term with regard to the preceding expression. Note
that you have to be careful with parens:
# These evaluate exit before doing the print:
print($foo, exit); # Obviously not what you want.
print $foo, exit; # Nor is this.
# These do the print before evaluating exit:
(print $foo), exit; # This is what you want.
print($foo), exit; # Or this.
print ($foo), exit; # Or even this.
Also note that
print ($foo & 255) + 1, "\n";
probably doesn't do what you expect at first glance.
File: perl.info, Node: Subroutines, Next: Passing By Reference, Prev: Precedence, Up: Top
Subroutines
***********
A subroutine may be declared as follows:
sub NAME BLOCK
Any arguments passed to the routine come in as array `@_', that is
`($_[0], $_[1], ...)'. The array `@_' is a local array, but its values
are references to the actual scalar parameters. The return value of
the subroutine is the value of the last expression evaluated, and can
be either an array value or a scalar value. Alternately, a return
statement may be used to specify the returned value and exit the
subroutine. To create local variables see the `local' operator.
A subroutine is called using the `do' operator or the `&' operator.
Example:
sub MAX {
local($max) = pop(@_);
foreach $foo (@_) {
$max = $foo if $max < $foo;
}
$max;
}
...
$bestday = &MAX($mon,$tue,$wed,$thu,$fri);
Example:
# get a line, combining continuation lines
# that start with whitespace
sub get_line {
$thisline = $lookahead;
line: while ($lookahead = <STDIN>) {
if ($lookahead =~ /^[ \t]/) {
$thisline .= $lookahead;
}
else {
last line;
}
}
$thisline;
}
$lookahead = <STDIN>; # get first line
while ($_ = do get_line()) {
...
}
Use array assignment to a local list to name your formal arguments:
sub maybeset {
local($key, $value) = @_;
$foo{$key} = $value unless $foo{$key};
}
This also has the effect of turning call-by-reference into
call-by-value, since the assignment copies the values.
Subroutines may be called recursively. If a subroutine is called
using the `&' form, the argument list is optional. If omitted, no `@_'
array is set up for the subroutine; the `@_' array at the time of the
call is visible to subroutine instead.
do foo(1,2,3); # pass three arguments
&foo(1,2,3); # the same
do foo(); # pass a null list
&foo(); # the same
&foo; # pass no arguments--more efficient
File: perl.info, Node: Passing By Reference, Next: Regular Expressions, Prev: Subroutines, Up: Top
Passing By Reference
********************
Sometimes you don't want to pass the value of an array to a
subroutine but rather the name of it, so that the subroutine can modify
the global copy of it rather than working with a local copy. In perl
you can refer to all the objects of a particular name by prefixing the
name with a star: `*foo'. When evaluated, it produces a scalar value
that represents all the objects of that name, including any filehandle,
format or subroutine. When assigned to within a `local()' operation,
it causes the name mentioned to refer to whatever `*' value was
assigned to it. Example:
sub doubleary {
local(*someary) = @_;
foreach $elem (@someary) {
$elem *= 2;
}
}
do doubleary(*foo);
do doubleary(*bar);
Assignment to `*name' is currently recommended only inside a
`local()'. You can actually assign to `*name' anywhere, but the
previous referent of `*name' may be stranded forever. This may or may
not bother you.
Note that scalars are already passed by reference, so you can modify
scalar arguments without using this mechanism by referring explicitly to
the `$_[nnn]' in question. You can modify all the elements of an array
by passing all the elements as scalars, but you have to use the `*'
mechanism to `push', `pop' or change the size of an array. The `*'
mechanism will probably be more efficient in any case.
Since a `*name' value contains unprintable binary data, if it is
used as an argument in a `print', or as a `%s' argument in a `printf'
or `sprintf', it then has the value `*name', just so it prints out
pretty.
Even if you don't want to modify an array, this mechanism is useful
for passing multiple arrays in a single LIST, since normally the LIST
mechanism will merge all the array values so that you can't extract out
the individual arrays.
File: perl.info, Node: Regular Expressions, Next: Formats, Prev: Passing By Reference, Up: Top
Regular Expressions
*******************
The patterns used in pattern matching are regular expressions such as
those supplied in the Version 8 regexp routines. (In fact, the routines
are derived from Henry Spencer's freely redistributable reimplementation
of the V8 routines.) In addition, `\w' matches an alphanumeric
character (including `_') and `\W' a nonalphanumeric. Word boundaries
may be matched by `\b', and non-boundaries by `\B'. A whitespace
character is matched by `\s', non-whitespace by `\S'. A numeric
character is matched by `\d', non-numeric by `\D'. You may use `\w',
`\s' and `\d' within character classes. Also, `\n', `\r', `\f', `\t'
and `\NNN' have their normal interpretations. Within character classes
`\b' represents backspace rather than a word boundary. Alternatives
may be separated by `|'. The bracketing construct `(...)' may also be
used, in which case `\<digit>' matches the digit'th substring.
(Outside of the pattern, always use `$' instead of `\' in front of the
digit. The scope of `$<digit>' (and `$`', `$&' and `$'') extends to
the end of the enclosing BLOCK or eval string, or to the next pattern
match with subexpressions. The `\<digit>' notation sometimes works
outside the current pattern, but should not be relied upon.) You may
have as many parentheses as you wish. If you have more than 9
substrings, the variables `$10', `$11', ... refer to the corresponding
substring. Within the pattern, `\10', `\11', etc. refer back to
substrings if there have been at least that many left parens before the
backreference. Otherwise (for backward compatibilty) `\10' is the same
as `\010', a backspace, and `\11' the same as `\011', a tab. And so
on. (`\1' through `\9' are always backreferences.)
`$+' returns whatever the last bracket match matched. `$&' returns
the entire matched string. (`$0' used to return the same thing, but
not any more.) `$`' returns everything before the matched string.
`$'' returns everything after the matched string. Examples:
s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words
if (/Time: (..):(..):(..)/) {
$hours = $1;
$minutes = $2;
$seconds = $3;
}
By default, the `^' character is only guaranteed to match at the
beginning of the string, the `$' character only at the end (or before
the newline at the end) and *perl* does certain optimizations with the
assumption that the string contains only one line. The behavior of `^'
and `$' on embedded newlines will be inconsistent. You may, however,
wish to treat a string as a multi-line buffer, such that the `^' will
match after any newline within the string, and `$' will match before
any newline. At the cost of a little more overhead, you can do this by
setting the variable `$*' to 1. Setting it back to 0 makes *perl*
revert to its old behavior.
To facilitate multi-line substitutions, the `.' character never
matches a newline (even when `$*' is 0). In particular, the following
leaves a newline on the `$_' string:
$_ = <STDIN>;
s/.*(some_string).*/$1/;
If the newline is unwanted, try one of
s/.*(some_string).*\n/$1/;
s/.*(some_string)[^\000]*/$1/;
s/.*(some_string)(.|\n)*/$1/;
chop; s/.*(some_string).*/$1/;
/(some_string)/ && ($_ = $1);
Any item of a regular expression may be followed with digits in curly
brackets of the form `{N,M}', where N gives the minimum number of times
to match the item and M gives the maximum. The form `{n}' is
equivalent to `{n,n}' and matches exactly N times. The form `{n,}'
matches N or more times. (If a curly bracket occurs in any other
context, it is treated as a regular character.) The `*' modifier is
equivalent to `{0,}', the `+' modifier to `{1,}' and the `?' modifier to
`{0,1}'. There is no limit to the size of N or M, but large numbers
will chew up more memory.
You will note that all backslashed metacharacters in *perl* are
alphanumeric, such as `\b', `\w', `\n'. Unlike some other regular
expression languages, there are no backslashed symbols that aren't
alphanumeric. So anything that looks like `\\', `\(', `\)', `\<',
`\>', `\{', or `\}' is always interpreted as a literal character, not a
metacharacter. This makes it simple to quote a string that you want to
use for a pattern but that you are afraid might contain metacharacters.
Simply quote all the non-alphanumeric characters:
$pattern =~ s/(\W)/\\$1/g;
File: perl.info, Node: Formats, Next: Interprocess Communication, Prev: Regular Expressions, Up: Top
Formats
*******
Output record formats for use with the `write' operator may declared
as follows:
format NAME =
FORMLIST
.
If name is omitted, format `STDOUT' is defined. FORMLIST consists
of a sequence of lines, each of which may be of one of three types:
1. A comment.
2. A "picture" line giving the format for one output line.
3. An argument line supplying values to plug into a picture line.
Picture lines are printed exactly as they look, except for certain
fields that substitute values into the line. Each picture field starts
with either `@' or `^'. The `@' field (not to be confused with the
array marker `@') is the normal case; `^' fields are used to do
rudimentary multi-line text block filling. The length of the field is
supplied by padding out the field with multiple `<', `>', or `|'
characters to specify, respectively, left justification, right
justification, or centering. As an alternate form of right
justification, you may also use `#' characters (with an optional `.')
to specify a numeric field. (Use of `^' instead of `@' causes the
field to be blanked if undefined.) If any of the values supplied for
these fields contains a newline, only the text up to the newline is
printed. The special field `@*' can be used for printing multi-line
values. It should appear by itself on a line.
The values are specified on the following line, in the same order as
the picture fields. The values should be separated by commas.
Picture fields that begin with `^' rather than `@' are treated
specially. The value supplied must be a scalar variable name which
contains a text string. *Perl* puts as much text as it can into the
field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed. Normally
you would use a sequence of fields in a vertical stack to print out a
block of text. If you like, you can end the final field with `...',
which will appear in the output if the text was too long to appear in
its entirety. You can change which characters are legal to break on by
changing the variable `$:' to a list of the desired characters.
Since use of `^' fields can produce variable length records if the
text to be formatted is short, you can suppress blank lines by putting
the tilde (`~') character anywhere in the line. (Normally you should
put it in the front if possible, for visibility.) The tilde will be
translated to a space upon output. If you put a second tilde
contiguous to the first, the line will be repeated until all the fields
on the line are exhausted. (If you use a field of the `@' variety, the
expression you supply had better not give the same value every time
forever!)
Examples:
# a report on the /etc/passwd file
format STDOUT_TOP =
Passwd File
Name Login Office Uid Gid Home
------------------------------------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
$name, $login, $office,$uid,$gid, $home
.
# a report from a bug report form
format STDOUT_TOP =
Bug Reports
@<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>>
$system, $%, $date
------------------------------------------------------------------
.
format STDOUT =
Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$subject
Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$index, $description
Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$priority, $date, $description
From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$from, $description
Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$programmer, $description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$description
~ ^<<<<<<<<<<<<<<<<<<<<<<<...
$description
.
It is possible to intermix `print's with `write's on the same output
channel, but you'll have to handle `$-' (lines left on the page)
yourself.
If you are printing lots of fields that are usually blank, you should
consider using the reset operator between records. Not only is it more
efficient, but it can prevent the bug of adding another field and
forgetting to zero it.
File: perl.info, Node: Interprocess Communication, Next: Predefined Names, Prev: Formats, Up: Top
Interprocess Communication
**************************
The IPC facilities of perl are built on the Berkeley socket
mechanism. If you don't have sockets, you can ignore this section.
The calls have the same names as the corresponding system calls, but
the arguments tend to differ, for two reasons. First, perl file
handles work differently than C file descriptors. Second, perl already
knows the length of its strings, so you don't need to pass that
information. Here is a sample client (untested):
($them,$port) = @ARGV;
$port = 2345 unless $port;
$them = 'localhost' unless $them;
$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child; }
require 'sys/socket.ph';
$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);
($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
unless $port =~ /^\d+$/;
($name, $aliases, $type, $len, $thisaddr) =
gethostbyname($hostname);
($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
$this = pack($sockaddr, &AF_INET, 0, $thisaddr);
$that = pack($sockaddr, &AF_INET, $port, $thataddr);
socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
bind(S, $this) || die "bind: $!";
connect(S, $that) || die "connect: $!";
select(S); $| = 1; select(stdout);
if ($child = fork) {
while (<>) {
print S;
}
sleep 3;
do dokill();
}
else {
while (<S>) {
print;
}
}
And here's a server:
($port) = @ARGV;
$port = 2345 unless $port;
require 'sys/socket.ph';
$sockaddr = 'S n a4 x8';
($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
unless $port =~ /^\d+$/;
$this = pack($sockaddr, &AF_INET, $port, "\0\0\0\0");
select(NS); $| = 1; select(stdout);
socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
bind(S, $this) || die "bind: $!";
listen(S, 5) || die "connect: $!";
select(S); $| = 1; select(stdout);
for (;;) {
print "Listening again\n";
($addr = accept(NS,S)) || die $!;
print "accept ok\n";
($af,$port,$inetaddr) = unpack($sockaddr,$addr);
@inetaddr = unpack('C4',$inetaddr);
print "$af $port @inetaddr\n";
while (<NS>) {
print;
print NS;
}
}
File: perl.info, Node: Predefined Names, Next: Packages, Prev: Interprocess Communication, Up: Top
Predefined Names
****************
The following names have special meaning to *perl*. I could have
used alphabetic symbols for some of these, but I didn't want to take the
chance that someone would say `reset "a-zA-Z"' and wipe them all out.
You'll just have to suffer along with these silly symbols. Most of
them have reasonable mnemonics, or analogues in one of the shells.
$_
The default input and pattern-searching space. The following
pairs are more or less equivalent:
while (<>) {... # only equivalent in while!
while ($_ = <>) {...
/^Subject:/
$_ =~ /^Subject:/
y/a-z/A-Z/
$_ =~ y/a-z/A-Z/
chop
chop($_)
(Mnemonic: underline is understood in certain operations.)
$.
The current input line number of the last filehandle that was read.
Readonly. Remember that only an explicit close on the filehandle
resets the line number. Since `<>' never does an explicit close,
line numbers increase across `ARGV' files (but see examples under
`eof'). (Mnemonic: many programs use `.' to mean the current line
number.)
$/
The input record separator, newline by default. Works like `awk''s
RS variable, including treating blank lines as delimiters if set
to the null string. You may set it to a multicharacter string to
match a multi-character delimiter. Note that setting it to
`"\n\n"' means something slightly different than setting it to
`""', if the file contains consecutive blank lines. Setting it to
`""' will treat two or more consecutive blank lines as a single
blank line. Setting it to "\en\en" will blindly assume that the
next input character belongs to the next paragraph, even if it's a
newline. (Mnemonic: `/' is used to delimit line boundaries when
quoting poetry.)
$,
The output field separator for the `print' operator. Ordinarily
the `print' operator simply prints out the comma separated fields
you specify. In order to get behavior more like `awk', set this
variable as you would set `awk''s OFS variable to specify what is
printed between fields. (Mnemonic: what is printed when there is
a `,' in your print statement.)
$"
This is like `$,' except that it applies to array values
interpolated into a double-quoted string (or similar interpreted
string). Default is a space. (Mnemonic: obvious, I think.)
$\
The output record separator for the `print' operator. Ordinarily
the `print' operator simply prints out the comma separated fields
you specify, with no trailing newline or record separator assumed.
In order to get behavior more like `awk', set this variable as
you would set `awk''s ORS variable to specify what is printed at
the end of the print. (Mnemonic: you set `$\' instead of adding
`\n' at the end of the print. Also, it's just like `/', but it's
what you get "back" from *perl*.)
$#
The output format for printed numbers. This variable is a
half-hearted attempt to emulate `awk''s OFMT variable. There are
times, however, when `awk' and *perl* have differing notions of
what is in fact numeric. Also, the initial value is `%.20g'
rather than `%.6g', so you need to set `$#' explicitly to get
`awk''s value. (Mnemonic: `#' is the number sign.)
$%
The current page number of the currently selected output channel.
(Mnemonic: `%' is page number in nroff.)
$=
The current page length (printable lines) of the currently selected
output channel. Default is 60. (Mnemonic: `=' has horizontal
lines.)
$-
The number of lines left on the page of the currently selected
output channel. (Mnemonic: lines_on_page - lines_printed.)
$~
The name of the current report format for the currently selected
output channel. Default is name of the filehandle. (Mnemonic:
brother to `$^'.)
$^
The name of the current top-of-page format for the currently
selected output channel. Default is name of the filehandle with
`_TOP' appended. (Mnemonic: points to top of page.)
$|
If set to nonzero, forces a flush after every `write' or `print'
on the currently selected output channel. Default is 0. Note that
`STDOUT' will typically be line buffered if output is to the
terminal and block buffered otherwise. Setting this variable is
useful primarily when you are outputting to a pipe, such as when
you are running a *perl* script under rsh and want to see the
output as it's happening. (Mnemonic: when you want your pipes to
be piping hot.)
$$
The process number of the *perl* running this script. (Mnemonic:
same as shells.)
$?
The status returned by the last pipe close, backtick (``) command
or `system' operator. Note that this is the status word returned
by the `wait()' system call, so the exit value of the subprocess is
actually `($? >> 8)'. `$? & 255' gives which signal, if any, the
process died from, and whether there was a core dump. (Mnemonic:
similar to `sh' and `ksh'.)
$&
The string matched by the last successful pattern match (not
counting any matches hidden within a BLOCK or `eval' enclosed by
the current BLOCK). (Mnemonic: like `&' in some editors.)
$`
The string preceding whatever was matched by the last successful
pattern match (not counting any matches hidden within a BLOCK or
`eval' enclosed by the current BLOCK). (Mnemonic: ` often
precedes a quoted string.)
$'
The string following whatever was matched by the last successful
pattern match (not counting any matches hidden within a BLOCK or
`eval' enclosed by the current BLOCK). (Mnemonic: ' often follows
a quoted string.) Example:
$_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n"; # prints abc:def:ghi
$+
The last bracket matched by the last search pattern. This is
useful if you don't know which of a set of alternative patterns
matched. For example:
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
(Mnemonic: be positive and forward looking.)
$*
Set to 1 to do multiline matching within a string, 0 to tell *perl*
that it can assume that strings contain a single line, for the
purpose of optimizing pattern matches. Pattern matches on strings
containing multiple newlines can produce confusing results when
`$*' is 0. Default is 0. Note that this variable only influences
the interpretation of `^' and `$'. A literal newline can be
searched for even when `$* == 0'. (Mnemonic: `*' matches multiple
things.)
$0
Contains the name of the file containing the *perl* script being
executed. Assigning to `$0' modifies the argument area that the
`ps(1)' program sees. (Mnemonic: same as `sh' and `ksh'.)
$<digit>
Contains the subpattern from the corresponding set of parentheses
in the last pattern matched, not counting patterns matched in
nested blocks that have been exited already. (Mnemonic: like
`\digit'.)
$[
The index of the first element in an array, and of the first
character in a substring. Default is 0, but you could set it to 1
to make *perl* behave more like `awk' (or Fortran) when
subscripting and when evaluating the `index()' and `substr()'
functions. (Mnemonic: `[' begins subscripts.)
$]
The string printed out when you say `perl -v'. It can be used to
determine at the beginning of a script whether the perl interpreter
executing the script is in the right range of versions. If used
in a numeric context, returns the version + patchlevel / 1000.
Example:
# see if getc is available
($version,$patchlevel) =
$] =~ /(\d+\.\d+).*\nPatch level: (\d+)/;
print STDERR "(No filename completion available.)\n"
if $version * 1000 + $patchlevel < 2016;
or, used numerically,
warn "No checksumming!\n" if $] < 3.019;
(Mnemonic: Is this version of perl in the right bracket?)
$;
The subscript separator for multi-dimensional array emulation. If
you refer to an associative array element as
$foo{$a,$b,$c}
it really means
$foo{join($;, $a, $b, $c)}
But don't put
@foo{$a,$b,$c} # a slice--note the @
which means
($foo{$a},$foo{$b},$foo{$c})
Default is `\034', the same as SUBSEP in `awk'. Note that if your
keys contain binary data there might not be any safe value for
`$;'. (Mnemonic: comma (the syntactic subscript separator) is a
semi-semicolon. Yeah, I know, it's pretty lame, but `$,' is
already taken for something more important.)
$!
If used in a numeric context, yields the current value of errno,
with all the usual caveats. (This means that you shouldn't depend
on the value of `$!' to be anything in particular unless you've
gotten a specific error return indicating a system error.) If
used in a string context, yields the corresponding system error
string. You can assign to `$!' in order to set errno if, for
instance, you want `$!' to return the string for error N, or you
want to set the exit value for the `die' operator. (Mnemonic:
What just went bang?)
$@
The perl syntax error message from the last `eval' command. If
null, the last `eval' parsed and executed correctly (although the
operations you invoked may have failed in the normal fashion).
(Mnemonic: Where was the syntax error "at"?)
$<
The real uid of this process. (Mnemonic: it's the uid you came
*FROM*, if you're running setuid.)
$>
The effective uid of this process. Example:
$< = $>; # set real uid to the effective uid
($<,$>) = ($>,$<); # swap real and effective uid
(Mnemonic: it's the uid you went *TO*, if you're running setuid.)
Note: `$<' and `$>' can only be swapped on machines supporting
`setreuid()'.
$(
The real gid of this process. If you are on a machine that
supports membership in multiple groups simultaneously, gives a
space separated list of groups you are in. The first number is
the one returned by `getgid()', and the subsequent ones by
`getgroups()', one of which may be the same as the first number.
(Mnemonic: parentheses are used to *GROUP* things. The real gid
is the group you *LEFT*, if you're running setgid.)
$)
The effective gid of this process. If you are on a machine that
supports membership in multiple groups simultaneously, gives a
space separated list of groups you are in. The first number is
the one returned by `getegid()', and the subsequent ones by
`getgroups()', one of which may be the same as the first number.
(Mnemonic: parentheses are used to *GROUP* things. The effective
gid is the group that's *RIGHT* for you, if you're running setgid.)
Note: `$<', `$>', `$(' and `$)' can only be set on machines that
support the corresponding `set[re][ug]id()' routine. `$(' and
`$)' can only be swapped on machines supporting `setregid()'.
$:
The current set of characters after which a string may be broken
to fill continuation fields (starting with `^') in a format.
Default is ` \n-', to break on whitespace or hyphens. (Mnemonic:
a "colon" in poetry is a part of a line.)
$^D
The current value of the debugging flags. (Mnemonic: value of `-D'
switch.)
$^F
The maximum system file descriptor, ordinarily 2. System file
descriptors are passed to subprocesses, while higher file
descriptors are not. During an open, system file descriptors are
preserved even if the open fails. Ordinary file descriptors are
closed before the open is attempted.
$^I
The current value of the inplace-edit extension. Use `undef' to
disable inplace editing. (Mnemonic: value of `-i' switch.)
$^L
What formats output to perform a formfeed. Default is `\f'.
$^P
The internal flag that the debugger clears so that it doesn't debug
itself. You could conceivably disable debugging yourself by
clearing it.
$^T
The time at which the script began running, in seconds since the
epoch. The values returned by the `-M', `-A', and `-C' filetests
are based on this value.
$^W
The current value of the warning switch. (Mnemonic: related to the
`-w' switch.)
$^X
The name that Perl itself was executed as, from argv[0].
$ARGV
The scalar variable `$ARGV' contains the name of the current file
when reading from `<>'.
@ARGV
The array `ARGV' contains the command line arguments intended for
the script. Note that `$#ARGV' is the generally number of
arguments minus one, since `$ARGV[0]' is the first argument, *NOT*
the command name. See `$0' for the command name.
@INC
The array `INC' contains the list of places to look for *perl*
scripts to be evaluated by the `do EXPR' command or the `require'
command. It initially consists of the arguments to any `-I'
command line switches, followed by the default *perl* library,
probably `/usr/local/lib/perl', followed by `.', to represent the
current directory.
%INC
The associative array `INC' contains entries for each filename that
has been included via `do' or `require'. The key is the filename
you specified, and the value is the location of the file actually
found. The `require' command uses this array to determine whether
a given file has already been included.
$ENV{expr}
The associative array `ENV' contains your current environment.
Setting a value in `ENV' changes the environment for child
processes.
$SIG{expr}
The associative array `SIG' is used to set signal handlers for
various signals. Example:
sub handler { # 1st argument is signal name
local($sig) = @_;
print "Caught a SIG$sig--shutting down\n";
close(LOG);
exit(0);
}
$SIG{'INT'} = 'handler';
$SIG{'QUIT'} = 'handler';
...
$SIG{'INT'} = 'DEFAULT'; # restore default action
$SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT
The `SIG' array only contains values for the signals actually set
within the perl script.
File: perl.info, Node: Packages, Next: Style, Prev: Predefined Names, Up: Top
Packages
********
Perl provides a mechanism for alternate namespaces to protect
packages from stomping on each others variables. By default, a perl
script starts compiling into the package known as `main'. By use of the
`package' declaration, you can switch namespaces. The scope of the
`package' declaration is from the declaration itself to the end of the
enclosing block (the same scope as the `local()' operator). Typically
it would be the first declaration in a file to be included by the
`require' operator. You can switch into a package in more than one
place; it merely influences which symbol table is used by the compiler
for the rest of that block. You can refer to variables and filehandles
in other packages by prefixing the identifier with the package name and
a single quote. If the package name is null, the `main' package as
assumed.
Only identifiers starting with letters are stored in the packages
symbol table. All other symbols are kept in package `main'. In
addition, the identifiers `STDIN', `STDOUT', `STDERR', `ARGV',
`ARGVOUT', `ENV', `INC' and `SIG' are forced to be in package `main',
even when used for other purposes than their built-in one. Note also
that, if you have a package called `m', `s' or `y', the you can't use
the qualified form of an identifier since it will be interpreted
instead as a pattern match, a substitution or a translation.
`eval''ed strings are compiled in the package in which the `eval'
was compiled in. (Assignments to `$SIG{}', however, assume the signal
handler specified is in the `main' package. Qualify the signal handler
name if you wish to have a signal handler in a package.) For an
example, examine `perldb.pl' in the perl library. It initially
switches to the `DB' package so that the debugger doesn't interfere
with variables in the script you are trying to debug. At various
points, however, it temporarily switches back to the `main' package to
evaluate various expressions in the context of the `main' package.
The symbol table for a package happens to be stored in the
associative array of that name prepended with an underscore. The value
in each entry of the associative array is what you are referring to
when you use the `*name' notation. In fact, the following have the
same effect (in package `main', anyway), though the first is more
efficient because it does the symbol table lookups at compile time:
local(*foo) = *bar;
local($_main{'foo'}) = $_main{'bar'};
You can use this to print out all the variables in a package, for
instance. Here is `dumpvar.pl' from the perl library:
package dumpvar;
sub main'dumpvar {
($package) = @_;
local(*stab) = eval("*_$package");
while (($key,$val) = each(%stab)) {
{
local(*entry) = $val;
if (defined $entry) {
print "\$$key = '$entry'\n";
}
if (defined @entry) {
print "\@$key = (\n";
foreach $num ($[ .. $#entry) {
print " $num\t'",$entry[$num],"'\n";
}
print ")\n";
}
if ($key ne "_$package" && defined %entry) {
print "\%$key = (\n";
foreach $key (sort keys(%entry)) {
print " $key\t'",$entry{$key},"'\n";
}
print ")\n";
}
}
}
}
Note that, even though the subroutine is compiled in package
`dumpvar', the name of the subroutine is qualified so that its name is
inserted into package `main'.
File: perl.info, Node: Style, Next: Debugging, Prev: Packages, Up: Top
Style
*****
Each programmer will, of course, have his or her own preferences in
regards to formatting, but there are some general guidelines that will
make your programs easier to read.
1. Just because you *CAN* do something a particular way doesn't mean
that you *SHOULD* do it that way. *Perl* is designed to give you
several ways to do anything, so consider picking the most readable
one. For instance
open(FOO,$foo) || die "Can't open $foo: $!";
is better than
die "Can't open $foo: $!" unless open(FOO,$foo);
because the second way hides the main point of the statement in a
modifier. On the other hand
print "Starting analysis\n" if $verbose;
is better than
$verbose && print "Starting analysis\n";
since the main point isn't whether the user typed `-v' or not.
Similarly, just because an operator lets you assume default
arguments doesn't mean that you have to make use of the defaults.
The defaults are there for lazy systems programmers writing
one-shot programs. If you want your program to be readable,
consider supplying the argument.
Along the same lines, just because you *can* omit parentheses in
many places doesn't mean that you ought to:
return print reverse sort num values array;
return print(reverse(sort num (values(%array))));
When in doubt, parenthesize. At the very least it will let some
poor schmuck bounce on the `%' key in *vi*.
Even if you aren't in doubt, consider the mental welfare of the
person who has to maintain the code after you, and who will
probably put parens in the wrong place.
2. Don't go through silly contortions to exit a loop at the top or
the bottom, when *perl* provides the `last' operator so you can
exit in the middle. Just outdent it a little to make it more
visible:
line:
for (;;) {
statements;
last line if $foo;
next line if /^#/;
statements;
}
3. Don't be afraid to use loop labels--they're there to enhance
readability as well as to allow multi-level loop breaks. See last
example.
4. For portability, when using features that may not be implemented
on every machine, test the construct in an `eval' to see if it
fails. If you know what version or patchlevel a particular
feature was implemented, you can test `$]' to see if it will be
there.
5. Choose mnemonic identifiers.
6. Be consistent.