📄 ch02.htm
字号:
You'll also learn about this topic later when we look at references in depth. A simple
example that implements an array of arrays follows:</P>
<PRE><FONT COLOR="#0066FF">@foo = ( 1, 2, 3);
@bar = ( 4, 5, 6);
@arrayref = (\@foo, \@bar);
print "The third element of \@foo is $arrayref[0][2]\n";
# prints: The third element of @foo is 3
</FONT></PRE>
<P>Here, I specify the zero'th element of @arrayref, which is an array of references
to arrays, and then use it to dereference the third element of @foo. @arrayref actually
looks and feels just like a two-dimensional array, but it's important to understand
that it isn't.</P>
<P>Perl does not support multidimensional arrays. All arrays are still flat. By using
references, however, you can emulate multidimensional arrays and build up complex
data structures. See PERLDSC for a compendium on Perl data structures.
<CENTER>
<H4><A NAME="Heading9"></A><FONT COLOR="#000077">Modules and Libraries</FONT></H4>
</CENTER>
<P>Perl5 provides you with a number of modules and extensions that contain packages
(classes). Using them simplifies and enhances the process of creating new Perl programs.
These modules are akin to C++ class libraries, in many ways, but are still simply
Perl packages, in the end. The older Perl4 libraries and packages are, in most cases,
still available, but many have been rewritten as modules where appropriate.</P>
<P>Working with Perl modules is the basis of this book. I supply very little code
that is new. The idea is that, once something has been developed to perform a task,
you should make use of it, or you're wasting your precious time.
<DL>
<DT></DT>
</DL>
<CENTER>
<H3>
<HR WIDTH="84%">
<BR>
<FONT COLOR="#000077">TIP:</FONT></H3>
</CENTER>
<BLOCKQUOTE>
<P>"Don't reinvent the wheel." Although this advice sounds trite, it's
important to the continued development of Perl5 as a viable object-oriented language.
The authors of the many useful Perl5 modules have spent a lot of time and effort
to provide their modules. Because this voluntary contribution is the foundation of
the Perl development effort, it should be nurtured and utilized to the fullest extent.
I suggest that you try to utilize these modules and help out where you can, by reporting
bugs and providing fixes and enhancements back to the author where appropriate.<BR>
<HR>
</BLOCKQUOTE>
<CENTER>
<H4><A NAME="Heading11"></A><FONT COLOR="#000077">Reusability</FONT></H4>
</CENTER>
<P>Using the new object-oriented features and techniques of Perl5 to develop new
programs enhances the potential for reusability of your code. Well-written programs
can be adapted to serve multiple purposes, possibly even being promoted to the status
of libraries or modules. (You'll learn how to register and/or submit your code as
a new module later in this chapter.)
<CENTER>
<H4><A NAME="Heading12"></A><FONT COLOR="#000077">Object-Oriented Capabilities</FONT></H4>
</CENTER>
<P>You can use the Perl module with references to achieve an object-oriented look
and feel in your programs. You can create inheritance (single and multiple), relationships,
virtual classes, constructors, destructors, and implement simple messaging with Perl5.</P>
<P>I cover some of these techniques within the extended portion of this tutorial.
If you're not familiar with object-oriented programming, refer to the many books
available, which describe the general techniques and the extended methodologies.
<CENTER>
<H4><A NAME="Heading13"></A><FONT COLOR="#000077">Extensible and Embeddable</FONT></H4>
</CENTER>
<P>The Perl modules provide a reusable interface for many commonly used programming
tasks. Many of the modules are also Perl extensions, which means that some component
of their interface is actually written using the Perl XSUB language. After the XSUB
code is translated to C, it is compiled with a C compiler. The functions in the C
code are then accessible as Perl subroutines in your program.</P>
<P>Perl5 is now embeddable, as well. A programmer can create a Perl interpreter in
any regular program written in C and, through the use of CallBacks and other internal
routines, interface to the Perl interpreter within his or her program. This capability
can provide an extremely powerful set of additional features for editors, servers,
and other tools.
<DL>
<DT></DT>
</DL>
<CENTER>
<H3>
<HR WIDTH="83%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
</CENTER>
<BLOCKQUOTE>
<P>The details regarding XSUB programming and creation of shared libraries are beyond
the scope of this tutorial. In this book, I don't tell you more than what you need
to use them in your Perl programs. I assume that they've been built and installed
on your machine. Likewise for embedded Perl, I only mention it here as a new feature.<BR>
<HR>
</BLOCKQUOTE>
<CENTER>
<H4><A NAME="Heading15"></A><FONT COLOR="#000077">Souped-Up Regular Expressions</FONT></H4>
</CENTER>
<P>The Perl regular expression--and its associated functions--remains one of the
most powerful and useful features of the overall language.</P>
<P><B>A Convention</B>
<BLOCKQUOTE>
<P>The words "Regular Expression" from here on may be shortened to "RE."
</BLOCKQUOTE>
<P>All of the older Perl4 RE functionality remains in Perl5, and several capabilities
have been added.</P>
<P>Possibly the most interesting and usable of the newer features is the extension
syntax that is now available for regular expressions. The extensions that are currently
available with Perl5 enable you to embed comments in your pattern, do grouping without
backreferences, perform zero-width positive lookahead assertions, and even have embedded
pattern-match modifiers. See PERLRE for details on all the powerful new RE features.
<CENTER>
<H4><A NAME="Heading16"></A><FONT COLOR="#000077">Enhanced Safety and Security</FONT></H4>
</CENTER>
<P>Perl5 integrates the TAINT features into the single Perl binary. All the runtime
checks and assurances remain when executing an SUID script, and you can even turn
on the TAINT features from the command line by using the -T switch. No additional
binaries need to be executed when running an SUID script. Naturally, this applies
only to architectures that support multiple user IDs, such as UNIX, and safe SUID
scripts. See PERLSEC for more details.
<DL>
<DT></DT>
</DL>
<P><B>Warning! SUIDPERL patch is necessary for older versions<TT>.</TT></B>
<BLOCKQUOTE>
<P>We'll mention this again in the security chapter, but it's important enough to
say right now. If you're using SUIDPERL, you need to be sure to apply the patch that
was released with Version 5.003. There's also a patch for older Perl4 SUIDPERL versions.
You can get the patch at your nearest CPAN.
</BLOCKQUOTE>
<P>A large number of new Perl modules provide the programmer with a rich new toolset
for dealing with security issues. Some modules provide encryption and WWW administrative
tasks, whereas other modules enhance standard system administration programming tasks.
Probably the most important security module is the Safe module, which allows you
to selectively enable or disable certain Perl operations within a program. You learn
more about these modules in Chapter 3, "Security on the Web."
<CENTER>
<H4><A NAME="Heading17"></A><FONT COLOR="#000077">Other New Features in Perl5</FONT></H4>
</CENTER>
<P>Now let's take a look at some other new features that you get with Perl5. Newer
ones are being considered on an ongoing basis, and may not be mentioned, but the
ones that follow are now formally part of the language. <B>BEGIN<TT> and </TT>END<TT>
Routines</TT></B> The BEGIN and END statements provide the scriptor with a means
to implement certain functionality in the Perl program as it is being compiled or
just after it exits. Anything you place within the BEGIN{} block is guaranteed to
be executed before any other statement in your program. It executes at the time the
program is being compiled. The following gives a simple example:</P>
<PRE><FONT COLOR="#0066FF">print "done that\n";
BEGIN { print "been there, "; }
# prints: been there, done that
</FONT></PRE>
<P>Likewise, anything you place in an END{} block is executed after every other statement
in the program has executed and just before Perl exits. Multiple END{} blocks are
executed in the reverse order of execution, as follows:</P>
<PRE><FONT COLOR="#0066FF">END { print "I\'ll see you in my dreams\n"; }
END { print "Irene. "; }
print "Goodnight, ";
# prints: Goodnight, Irene. I'll see you in my dreams.
</FONT></PRE>
<P><B>use()<TT> and </TT>no()<TT> Statements</TT></B> The use() statement imports
symbols and/or semantics from the named package into your program by aliasing subroutines
and/or variable names from the package into the current package's namespace. When
you say</P>
<PRE><FONT COLOR="#0066FF">use Module;
</FONT></PRE>
<P>in your program, it's the same as saying</P>
<PRE><FONT COLOR="#0066FF">BEGIN{ require Module; import Module List};
</FONT></PRE>
<P>where Module has exported some List of methods (subroutines) via its @EXPORT or
@EXPORT_OK <BR>
arrays, and you're making them part of your current package namespace (main, in this
case). Note that the import method is not a builtin, but is actually a method itself,
from the Exporter module. Much more on this later in this chapter.</P>
<P>You can also specify that nothing be imported by giving an empty list to use(),
like this:</P>
<PRE><FONT COLOR="#0066FF">use SomeModule ();
</FONT></PRE>
<P>Then you have access to all the methods of the package but only via the full name
of the method or a blessed reference to the package. I discuss these details later
in the chapter.</P>
<P>When you use one or more of the pragmatic modules, the use() statement imports
semantics instead of symbols. For instance,</P>
<PRE><FONT COLOR="#0066FF">use strict subs;
</FONT></PRE>
<P>This statement causes a compile-time error if you try to use a bare word identifier
that isn't a subroutine, unless it appears in curly braces or on the left side of
the => operator. These pragmas are then in effect through the end of the file
or until the corresponding no() statement turns them off, as in the following:</P>
<PRE><FONT COLOR="#0066FF">no strict subs;
</FONT></PRE>
<P>See PERLMOD and PERLDSC for more details on the pragmatic modules, which were
previously mentioned when describing the new warnings and stricture, and PERLVAR
for the official definition of use() and no(). There's also the POD within strict.pm
for full documentation of the strict pragmas. The strict.pm module is installed with
the rest of the Perl modules in @INC. <B><TT>The New </TT>::<TT> Operator and the
</TT>-><TT> Operator</TT></B> These :: and -> operators are provided as a means
for invoking the methods within a given package. You should use the Package::subroutine()
syntax instead of the older, deprecated Package'subroutine() syntax; this new syntax
provides the same semantics. This operator also works when you're accessing variables
within a package, such as $Package::scalar, @Package::array, and %Package::hash,
just like the older, single tick operator does. Again though, the single tick operator
may not be part of the language forever. It's better to use the :: operator in all
new code.</P>
<P>The -> operator provides a means to invoke the methods of a class or package,
along with any methods of any of its parent classes, using a blessed reference to
the package as well as serving as a post-fix dereference operator for any general
reference. You will learn about this operator in detail later in this chapter. <B>\U<TT>,
</TT>\L<TT>, </TT>uc()<TT>, </TT>lc()<TT>, </TT>ucfirst()<TT>, and </TT>lcfirst()<TT>
Operators</TT></B> The \U, \L, uc(), lc(), ucfirst(), and lcfirst() operators enable
you to operate on strings, modifying them to be all uppercase, all lowercase, or
have just the first letter of the string be uppercase. The \U and \L operators work
within the double-quoted string, and the others take strings as arguments and return
modified copies. For example,</P>
<PRE><FONT COLOR="#0066FF">$ucstring = "FOO";
$lcstring = lc($ucstring);
print $ucstring, ` `, $lcstring, ` `, ucfirst($lcstring),"\n"
#prints:
FOO foo Foo
</FONT></PRE>
<P><B><TT>Closures</TT></B> A closure is implemented as an anonymous subroutine or
a reference to a subroutine. It is generally declared using a reference, which is
then used to invoke the closure. Consider the following example:</P>
<PRE><FONT COLOR="#0066FF">sub newanon{
my $foo = shift;
my $anonsub = sub {
my $arg = shift;
print "Hey, I'm in an $foo $arg\n";
};
return $anonsub;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -