perldata.pod
来自「ARM上的如果你对底层感兴趣」· POD 代码 · 共 604 行 · 第 1/2 页
POD
604 行
ignored, but may be read via a DATA filehandle: main::DATA for __END__,
or PACKNAME::DATA (where PACKNAME is the current package) for __DATA__.
The two control characters ^D and ^Z are synonyms for __END__ (or
__DATA__ in a module). See L<SelfLoader> for more description of
__DATA__, and an example of its use. Note that you cannot read from the
DATA filehandle in a BEGIN block: the BEGIN block is executed as soon as
it is seen (during compilation), at which point the corresponding
__DATA__ (or __END__) token has not yet been seen.
A word that has no other interpretation in the grammar will
be treated as if it were a quoted string. These are known as
"barewords". As with filehandles and labels, a bareword that consists
entirely of lowercase letters risks conflict with future reserved
words, and if you use the B<-w> switch, Perl will warn you about any
such words. Some people may wish to outlaw barewords entirely. If you
say
use strict 'subs';
then any bareword that would NOT be interpreted as a subroutine call
produces a compile-time error instead. The restriction lasts to the
end of the enclosing block. An inner block may countermand this
by saying C<no strict 'subs'>.
Array variables are interpolated into double-quoted strings by joining all
the elements of the array with the delimiter specified in the C<$">
variable (C<$LIST_SEPARATOR> in English), space by default. The following
are equivalent:
$temp = join($",@ARGV);
system "echo $temp";
system "echo @ARGV";
Within search patterns (which also undergo double-quotish substitution)
there is a bad ambiguity: Is C</$foo[bar]/> to be interpreted as
C</${foo}[bar]/> (where C<[bar]> is a character class for the regular
expression) or as C</${foo[bar]}/> (where C<[bar]> is the subscript to array
@foo)? If @foo doesn't otherwise exist, then it's obviously a
character class. If @foo exists, Perl takes a good guess about C<[bar]>,
and is almost always right. If it does guess wrong, or if you're just
plain paranoid, you can force the correct interpretation with curly
brackets as above.
A line-oriented form of quoting is based on the shell "here-doc"
syntax. Following a C<E<lt>E<lt>> you specify a string to terminate
the quoted material, and all lines following the current line down to
the terminating string are the value of the item. The terminating
string may be either an identifier (a word), or some quoted text. If
quoted, the type of quotes you use determines the treatment of the
text, just as in regular quoting. An unquoted identifier works like
double quotes. There must be no space between the C<E<lt>E<lt>> and
the identifier. (If you put a space it will be treated as a null
identifier, which is valid, and matches the first empty line.) The
terminating string must appear by itself (unquoted and with no
surrounding whitespace) on the terminating line.
print <<EOF;
The price is $Price.
EOF
print <<"EOF"; # same as above
The price is $Price.
EOF
print <<`EOC`; # execute commands
echo hi there
echo lo there
EOC
print <<"foo", <<"bar"; # you can stack them
I said foo.
foo
I said bar.
bar
myfunc(<<"THIS", 23, <<'THAT');
Here's a line
or two.
THIS
and here's another.
THAT
Just don't forget that you have to put a semicolon on the end
to finish the statement, as Perl doesn't know you're not going to
try to do this:
print <<ABC
179231
ABC
+ 20;
=head2 List value constructors
List values are denoted by separating individual values by commas
(and enclosing the list in parentheses where precedence requires it):
(LIST)
In a context not requiring a list value, the value of the list
literal is the value of the final element, as with the C comma operator.
For example,
@foo = ('cc', '-E', $bar);
assigns the entire list value to array foo, but
$foo = ('cc', '-E', $bar);
assigns the value of variable bar to variable foo. Note that the value
of an actual array in a scalar context is the length of the array; the
following assigns the value 3 to $foo:
@foo = ('cc', '-E', $bar);
$foo = @foo; # $foo gets 3
You may have an optional comma before the closing parenthesis of a
list literal, so that you can say:
@foo = (
1,
2,
3,
);
LISTs do automatic interpolation of sublists. That is, when a LIST is
evaluated, each element of the list is evaluated in a list context, and
the resulting list value is interpolated into LIST just as if each
individual element were a member of LIST. Thus arrays and hashes lose their
identity in a LIST--the list
(@foo,@bar,&SomeSub,%glarch)
contains all the elements of @foo followed by all the elements of @bar,
followed by all the elements returned by the subroutine named SomeSub
called in a list context, followed by the key/value pairs of %glarch.
To make a list reference that does I<NOT> interpolate, see L<perlref>.
The null list is represented by (). Interpolating it in a list
has no effect. Thus ((),(),()) is equivalent to (). Similarly,
interpolating an array with no elements is the same as if no
array had been interpolated at that point.
A list value may also be subscripted like a normal array. You must
put the list in parentheses to avoid ambiguity. For example:
# Stat returns list value.
$time = (stat($file))[8];
# SYNTAX ERROR HERE.
$time = stat($file)[8]; # OOPS, FORGOT PARENTHESES
# Find a hex digit.
$hexdigit = ('a','b','c','d','e','f')[$digit-10];
# A "reverse comma operator".
return (pop(@foo),pop(@foo))[0];
You may assign to C<undef> in a list. This is useful for throwing
away some of the return values of a function:
($dev, $ino, undef, undef, $uid, $gid) = stat($file);
Lists may be assigned to if and only if each element of the list
is legal to assign to:
($a, $b, $c) = (1, 2, 3);
($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
Array assignment in a scalar context returns the number of elements
produced by the expression on the right side of the assignment:
$x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2
$x = (($foo,$bar) = f()); # set $x to f()'s return count
This is very handy when you want to do a list assignment in a Boolean
context, because most list functions return a null list when finished,
which when assigned produces a 0, which is interpreted as FALSE.
The final element may be an array or a hash:
($a, $b, @rest) = split;
my($a, $b, %rest) = @_;
You can actually put an array or hash anywhere in the list, but the first one
in the list will soak up all the values, and anything after it will get
a null value. This may be useful in a local() or my().
A hash literal contains pairs of values to be interpreted
as a key and a value:
# same as map assignment above
%map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
While literal lists and named arrays are usually interchangeable, that's
not the case for hashes. Just because you can subscript a list value like
a normal array does not mean that you can subscript a list value as a
hash. Likewise, hashes included as parts of other lists (including
parameters lists and return lists from functions) always flatten out into
key/value pairs. That's why it's good to use references sometimes.
It is often more readable to use the C<=E<gt>> operator between key/value
pairs. The C<=E<gt>> operator is mostly just a more visually distinctive
synonym for a comma, but it also arranges for its left-hand operand to be
interpreted as a string--if it's a bareword that would be a legal identifier.
This makes it nice for initializing hashes:
%map = (
red => 0x00f,
blue => 0x0f0,
green => 0xf00,
);
or for initializing hash references to be used as records:
$rec = {
witch => 'Mable the Merciless',
cat => 'Fluffy the Ferocious',
date => '10/31/1776',
};
or for using call-by-named-parameter to complicated functions:
$field = $query->radio_group(
name => 'group_name',
values => ['eenie','meenie','minie'],
default => 'meenie',
linebreak => 'true',
labels => \%labels
);
Note that just because a hash is initialized in that order doesn't
mean that it comes out in that order. See L<perlfunc/sort> for examples
of how to arrange for an output ordering.
=head2 Typeglobs and Filehandles
Perl uses an internal type called a I<typeglob> to hold an entire
symbol table entry. The type prefix of a typeglob is a C<*>, because
it represents all types. This used to be the preferred way to
pass arrays and hashes by reference into a function, but now that
we have real references, this is seldom needed.
The main use of typeglobs in modern Perl is create symbol table aliases.
This assignment:
*this = *that;
makes $this an alias for $that, @this an alias for @that, %this an alias
for %that, &this an alias for &that, etc. Much safer is to use a reference.
This:
local *Here::blue = \$There::green;
temporarily makes $Here::blue an alias for $There::green, but doesn't
make @Here::blue an alias for @There::green, or %Here::blue an alias for
%There::green, etc. See L<perlmod/"Symbol Tables"> for more examples
of this. Strange though this may seem, this is the basis for the whole
module import/export system.
Another use for typeglobs is to to pass filehandles into a function or
to create new filehandles. If you need to use a typeglob to save away
a filehandle, do it this way:
$fh = *STDOUT;
or perhaps as a real reference, like this:
$fh = \*STDOUT;
See L<perlsub> for examples of using these as indirect filehandles
in functions.
Typeglobs are also a way to create a local filehandle using the local()
operator. These last until their block is exited, but may be passed back.
For example:
sub newopen {
my $path = shift;
local *FH; # not my!
open (FH, $path) or return undef;
return *FH;
}
$fh = newopen('/etc/passwd');
Now that we have the *foo{THING} notation, typeglobs aren't used as much
for filehandle manipulations, although they're still needed to pass brand
new file and directory handles into or out of functions. That's because
*HANDLE{IO} only works if HANDLE has already been used as a handle.
In other words, *FH can be used to create new symbol table entries,
but *foo{THING} cannot.
Another way to create anonymous filehandles is with the IO::Handle
module and its ilk. These modules have the advantage of not hiding
different types of the same name during the local(). See the bottom of
L<perlfunc/open()> for an example.
See L<perlref>, L<perlsub>, and L<perlmod/"Symbol Tables"> for more
discussion on typeglobs and the *foo{THING} syntax.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?