[ Pobierz całość w formacie PDF ]

A subroutine is called using the do operator or the & operator.
Example:
sub MAX {
local($max) = pop(@_);
foreach $foo (@_) {
$max = $foo if $max
}
$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 = ) {
if ($lookahead =~/^[ \^\t]/) {
$thisline .= $lookahead;
}
else {
last line;
}
}
$thisline;
}
$lookahead = ; # 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
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.
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 \ matches the digit th substring. (Outside of the pattern, always use $ instead of \ in
front of the digit. The scope of $ (and $\ , $& and $ ) extends to the end of the enclosing BLOCK
or eval string, or to the next pattern match with subexpressions. The \ 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 [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • fopke.keep.pl