📄 perl5004delta.pod
字号:
foreach my $i (1, 2, 3) {
some_function();
}
$i is a lexical variable, and the scope of $i extends to the end of
the loop, but not beyond it.
Note that you still cannot use my() on global punctuation variables
such as $_ and the like.
=item pack() and unpack()
A new format 'w' represents a BER compressed integer (as defined in
ASN.1). Its format is a sequence of one or more bytes, each of which
provides seven bits of the total value, with the most significant
first. Bit eight of each byte is set, except for the last byte, in
which bit eight is clear.
If 'p' or 'P' are given undef as values, they now generate a NULL
pointer.
Both pack() and unpack() now fail when their templates contain invalid
types. (Invalid types used to be ignored.)
=item sysseek()
The new sysseek() operator is a variant of seek() that sets and gets the
file's system read/write position, using the lseek(2) system call. It is
the only reliable way to seek before using sysread() or syswrite(). Its
return value is the new position, or the undefined value on failure.
=item use VERSION
If the first argument to C<use> is a number, it is treated as a version
number instead of a module name. If the version of the Perl interpreter
is less than VERSION, then an error message is printed and Perl exits
immediately. Because C<use> occurs at compile time, this check happens
immediately during the compilation process, unlike C<require VERSION>,
which waits until runtime for the check. This is often useful if you
need to check the current Perl version before C<use>ing library modules
which have changed in incompatible ways from older versions of Perl.
(We try not to do this more than we have to.)
=item use Module VERSION LIST
If the VERSION argument is present between Module and LIST, then the
C<use> will call the VERSION method in class Module with the given
version as an argument. The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable $Module::VERSION. (Note that there is not a
comma after VERSION!)
This version-checking mechanism is similar to the one currently used
in the Exporter module, but it is faster and can be used with modules
that don't use the Exporter. It is the recommended method for new
code.
=item prototype(FUNCTION)
Returns the prototype of a function as a string (or C<undef> if the
function has no prototype). FUNCTION is a reference to or the name of the
function whose prototype you want to retrieve.
(Not actually new; just never documented before.)
=item srand
The default seed for C<srand>, which used to be C<time>, has been changed.
Now it's a heady mix of difficult-to-predict system-dependent values,
which should be sufficient for most everyday purposes.
Previous to version 5.004, calling C<rand> without first calling C<srand>
would yield the same sequence of random numbers on most or all machines.
Now, when perl sees that you're calling C<rand> and haven't yet called
C<srand>, it calls C<srand> with the default seed. You should still call
C<srand> manually if your code might ever be run on a pre-5.004 system,
of course, or if you want a seed other than the default.
=item $_ as Default
Functions documented in the Camel to default to $_ now in
fact do, and all those that do are so documented in L<perlfunc>.
=item C<m//gc> does not reset search position on failure
The C<m//g> match iteration construct has always reset its target
string's search position (which is visible through the C<pos> operator)
when a match fails; as a result, the next C<m//g> match after a failure
starts again at the beginning of the string. With Perl 5.004, this
reset may be disabled by adding the "c" (for "continue") modifier,
i.e. C<m//gc>. This feature, in conjunction with the C<\G> zero-width
assertion, makes it possible to chain matches together. See L<perlop>
and L<perlre>.
=item C<m//x> ignores whitespace before ?*+{}
The C<m//x> construct has always been intended to ignore all unescaped
whitespace. However, before Perl 5.004, whitespace had the effect of
escaping repeat modifiers like "*" or "?"; for example, C</a *b/x> was
(mis)interpreted as C</a\*b/x>. This bug has been fixed in 5.004.
=item nested C<sub{}> closures work now
Prior to the 5.004 release, nested anonymous functions didn't work
right. They do now.
=item formats work right on changing lexicals
Just like anonymous functions that contain lexical variables
that change (like a lexical index variable for a C<foreach> loop),
formats now work properly. For example, this silently failed
before (printed only zeros), but is fine now:
my $i;
foreach $i ( 1 .. 10 ) {
write;
}
format =
my i is @#
$i
.
However, it still fails (without a warning) if the foreach is within a
subroutine:
my $i;
sub foo {
foreach $i ( 1 .. 10 ) {
write;
}
}
foo;
format =
my i is @#
$i
.
=back
=head2 New builtin methods
The C<UNIVERSAL> package automatically contains the following methods that
are inherited by all other classes:
=over
=item isa(CLASS)
C<isa> returns I<true> if its object is blessed into a subclass of C<CLASS>
C<isa> is also exportable and can be called as a sub with two arguments. This
allows the ability to check what a reference points to. Example:
use UNIVERSAL qw(isa);
if(isa($ref, 'ARRAY')) {
...
}
=item can(METHOD)
C<can> checks to see if its object has a method called C<METHOD>,
if it does then a reference to the sub is returned; if it does not then
I<undef> is returned.
=item VERSION( [NEED] )
C<VERSION> returns the version number of the class (package). If the
NEED argument is given then it will check that the current version (as
defined by the $VERSION variable in the given package) not less than
NEED; it will die if this is not the case. This method is normally
called as a class method. This method is called automatically by the
C<VERSION> form of C<use>.
use A 1.2 qw(some imported subs);
# implies:
A->VERSION(1.2);
=back
B<NOTE:> C<can> directly uses Perl's internal code for method lookup, and
C<isa> uses a very similar method and caching strategy. This may cause
strange effects if the Perl code dynamically changes @ISA in any package.
You may add other methods to the UNIVERSAL class via Perl or XS code.
You do not need to C<use UNIVERSAL> in order to make these methods
available to your program. This is necessary only if you wish to
have C<isa> available as a plain subroutine in the current package.
=head2 TIEHANDLE now supported
See L<perltie> for other kinds of tie()s.
=over
=item TIEHANDLE classname, LIST
This is the constructor for the class. That means it is expected to
return an object of some sort. The reference can be used to
hold some internal information.
sub TIEHANDLE {
print "<shout>\n";
my $i;
return bless \$i, shift;
}
=item PRINT this, LIST
This method will be triggered every time the tied handle is printed to.
Beyond its self reference it also expects the list that was passed to
the print function.
sub PRINT {
$r = shift;
$$r++;
return print join( $, => map {uc} @_), $\;
}
=item PRINTF this, LIST
This method will be triggered every time the tied handle is printed to
with the C<printf()> function.
Beyond its self reference it also expects the format and list that was
passed to the printf function.
sub PRINTF {
shift;
my $fmt = shift;
print sprintf($fmt, @_)."\n";
}
=item READ this LIST
This method will be called when the handle is read from via the C<read>
or C<sysread> functions.
sub READ {
$r = shift;
my($buf,$len,$offset) = @_;
print "READ called, \$buf=$buf, \$len=$len, \$offset=$offset";
}
=item READLINE this
This method will be called when the handle is read from. The method
should return undef when there is no more data.
sub READLINE {
$r = shift;
return "PRINT called $$r times\n"
}
=item GETC this
This method will be called when the C<getc> function is called.
sub GETC { print "Don't GETC, Get Perl"; return "a"; }
=item DESTROY this
As with the other types of ties, this method will be called when the
tied handle is about to be destroyed. This is useful for debugging and
possibly for cleaning up.
sub DESTROY {
print "</shout>\n";
}
=back
=head2 Malloc enhancements
If perl is compiled with the malloc included with the perl distribution
(that is, if C<perl -V:d_mymalloc> is 'define') then you can print
memory statistics at runtime by running Perl thusly:
env PERL_DEBUG_MSTATS=2 perl your_script_here
The value of 2 means to print statistics after compilation and on
exit; with a value of 1, the statistics are printed only on exit.
(If you want the statistics at an arbitrary time, you'll need to
install the optional module Devel::Peek.)
Three new compilation flags are recognized by malloc.c. (They have no
effect if perl is compiled with system malloc().)
=over
=item -DPERL_EMERGENCY_SBRK
If this macro is defined, running out of memory need not be a fatal
error: a memory pool can allocated by assigning to the special
variable C<$^M>. See L<"$^M">.
=item -DPACK_MALLOC
Perl memory allocation is by bucket with sizes close to powers of two.
Because of these malloc overhead may be big, especially for data of
size exactly a power of two. If C<PACK_MALLOC> is defined, perl uses
a slightly different algorithm for small allocations (up to 64 bytes
long), which makes it possible to have overhead down to 1 byte for
allocations which are powers of two (and appear quite often).
Expected memory savings (with 8-byte alignment in C<alignbytes>) is
about 20% for typical Perl usage. Expected slowdown due to additional
malloc overhead is in fractions of a percent (hard to measure, because
of the effect of saved memory on speed).
=item -DTWO_POT_OPTIMIZE
Similarly to C<PACK_MALLOC>, this macro improves allocations of data
with size close to a power of two; but this works for big allocations
(starting with 16K by default). Such allocations are typical for big
hashes and special-purpose scripts, especially image processing.
On recent systems, the fact that perl requires 2M from system for 1M
allocation will not affect speed of execution, since the tail of such
a chunk is not going to be touched (and thus will not require real
memory). However, it may result in a premature out-of-memory error.
So if you will be manipulating very large blocks with sizes close to
powers of two, it would be wise to define this macro.
Expected saving of memory is 0-100% (100% in applications which
require most memory in such 2**n chunks); expected slowdown is
negligible.
=back
=head2 Miscellaneous efficiency enhancements
Functions that have an empty prototype and that do nothing but return
a fixed value are now inlined (e.g. C<sub PI () { 3.14159 }>).
Each unique hash key is only allocated once, no matter how many hashes
have an entry with that key. So even if you have 100 copies of the
same hash, the hash keys never have to be reallocated.
=head1 Support for More Operating Systems
Support for the following operating systems is new in Perl 5.004.
=head2 Win32
Perl 5.004 now includes support for building a "native" perl under
Windows NT, using the Microsoft Visual C++ compiler (versions 2.0
and above) or the Borland C++ compiler (versions 5.02 and above).
The resulting perl can be used under Windows 95 (if it
is installed in the same directory locations as it got installed
in Windows NT). This port includes support for perl extension
building tools like L<MakeMaker> and L<h2xs>, so that many extensions
available on the Comprehensive Perl Archive Network (CPAN) can now be
readily built under Windows NT. See http://www.perl.com/ for more
information on CPAN and F<README.win32> in the perl distribution for more
details on how to get started with building this port.
There is also support for building perl under the Cygwin32 environment.
Cygwin32 is a set of GNU tools that make it possible to compile and run
many UNIX programs under Windows NT by providing a mostly UNIX-like
interface for compilation and execution. See F<README.cygwin32> in the
perl distribution for more details on this port and how to obtain the
Cygwin32 toolkit.
=head2 Plan 9
See F<README.plan9> in the perl distribution.
=head2 QNX
See F<README.qnx> in the perl distribution.
=head2 AmigaOS
See F<README.amigaos> in the perl distribution.
=head1 Pragmata
Six new pragmatic modules exist:
=over
=item use autouse MODULE => qw(sub1 sub2 sub3)
Defers C<require MODULE> until someone calls one of the specified
subroutines (which must be exported by MODULE). This pragma should be
used with caution, and only when necessary.
=item use blib
=item use blib 'dir'
Looks for MakeMaker-like I<'blib'> directory structure starting in
I<dir> (or current directory) and working back up to five levels of
parent directories.
Intended for use on command line with B<-M> option as a way of testing
arbitrary scripts against an uninstalled version of a package.
=item use constant NAME => VALUE
Provides a convenient interface for creating compile-time constants,
See L<perlsub/"Constant Functions">.
=item use locale
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -