⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
		<TD WIDTH="97" ALIGN="LEFT">PERLLOL</TD>



		<TD ALIGN="LEFT">Perl data structures: lists of lists</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLOBJ</TD>



		<TD ALIGN="LEFT">Perl objects</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLTIE</TD>



		<TD ALIGN="LEFT">Perl objects hidden behind simple variables</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLBOT</TD>



		<TD ALIGN="LEFT">Perl OO tricks and examples</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLIPC</TD>



		<TD ALIGN="LEFT">Perl interprocess communication</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLDEBUG</TD>



		<TD ALIGN="LEFT">Perl debugging</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLDIAG</TD>



		<TD ALIGN="LEFT">Perl diagnostic messages</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLSEC</TD>



		<TD ALIGN="LEFT">Perl security</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLTRAP</TD>



		<TD ALIGN="LEFT">Perl traps for the unwary</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLSTYLE</TD>



		<TD ALIGN="LEFT">Perl style guide</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLXS</TD>



		<TD ALIGN="LEFT">Perl XS application programming interface</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLXSTUT</TD>



		<TD ALIGN="LEFT">Perl XS tutorial</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLGUTS</TD>



		<TD ALIGN="LEFT">Perl internal functions for creating extensions</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLCALL</TD>



		<TD ALIGN="LEFT">Perl calling conventions from C</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLEMBED</TD>



		<TD ALIGN="LEFT">Perl: how to embed Perl in your C or C++ application</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLPOD</TD>



		<TD ALIGN="LEFT">Perl plain old documentation</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLAPIO</TD>



		<TD ALIGN="LEFT">Perl internal IO abstraction interface</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD WIDTH="97" ALIGN="LEFT">PERLBOOK</TD>



		<TD ALIGN="LEFT">Perl book information</TD>



	</TR>



</TABLE>



<BR>



<BR>



The documentation also contains numerous additional sections corresponding to the



standard modules that ship with Perl. Most or all of these additional sections are



extracted from the embedded POD, which is to be found in the module file itself.



All Perl documentation is written first in POD and then translated to other formats.







<DL>



	<DT></DT>



</DL>







<P><B><TT>POD</TT></B>











<BLOCKQUOTE>



	<P>Plain Old Documentation. ASCII text documentation with markers corresponding to



	the various formatting elements. Can be embedded directly into Perl modules. See



	PERLPOD.







</BLOCKQUOTE>







<P>You can easily transform POD into standard UNIX *ROFF format, HTML, and a number



of other formats by using the pod2* (pod2man, pod2html, pod2text, and so on) converters.



There exist POD converters to many other types of formats, as well. The POD format



also implies that you can read the documentation directly, without any post-formatting



at all. Everything that I cover in this chapter is also documented in the Perl PODs,



and I give references to the specific sections as I go along, using the PERLBLAH



notation as previously mentioned. POD can be embedded directly into a Perl module,



or program, and nearly all of the modules have them already.</P>



<P>When Perl is installed on a typical UNIX site, the POD documentation, including



POD from the modules, is converted automatically into standard UNIX manpages. The



administrator usually installs it inside the primary @INC directory, usually in a



subdirectory called man. Macintosh Perl installations have the POD, converted to



HTML format, in a folder beneath the folder that contains the Perl application, named



pod. The Windows (ntperl) installation also has the documentation, converted to HTML,



in the directory called docs. You should find this directory, and be ready to refer



to the documentation within it.</P>



<P>As I mentioned previously, conversion tools are available for POD, including pod2html,



pod2text, pod2rtf, pod2tex, pod2inf, and now even pod2ps, and the pod2man program.



You can use any of them to convert from POD format to your preferred format, provided



that the tool has been written to work on your architecture. All these tools work



on UNIX, and some are configurable to work on other platforms.







<DL>



	<DT></DT>



</DL>







<P><B>A Note on Compatibility</B>











<BLOCKQUOTE>



	<P>There are, as you might guess, still a few incompatibilities when attempting to



	produce cross-platform Perl code, and not all scripts run on all architectures by



	default. I'll try to note these, as I progress through this chapter and through the



	book. Filepaths, for instance, shouldn't be hardcoded in Perl programs, but often



	are. This issue in general is being considered and worked on by the Perl Porters,



	a large group of brilliant people who help to bring you Perl.







</BLOCKQUOTE>







<P>Remember to check your favorite CPAN to get the latest versions of the pod2* programs.







<DL>



	<DT></DT>



</DL>







<P><B><TT>CPAN</TT></B>











<BLOCKQUOTE>



	<P>Comprehensive Perl Archive Network. A large group of well-connected Internet sites



	that maintains a copy of the master Perl archive. You can find more details later



	in this chapter and a complete history and description of the CPAN in Chapter 1.







</BLOCKQUOTE>







<P><B><TT>Readability Improvements</TT></B> The ability to provide easily readable



and reusable code has become more important as the level of formal training and skills



required to start a Web site has decreased. The responsibility is largely up to the



script author to implement this readability. Perl5 also provides some new functionality



that enhances the capability of the script author to do so.</P>



<P>The English module increases the readability and understanding of Perl code, and



it is a big step toward alleviating the boggling effect that raw Perl code sometimes



has on new programmers. The English module provides a mapping between Perl's eclectic



punctuation (special) variables with an English name corresponding to each one. The



regular-expression variables that correspond to the three components of a matched



string, for example, are often difficult to remember, even for the experienced Perl



programmer. The English module maps these variables as follows:</P>



<PRE><FONT COLOR="#0066FF">*MATCH = *&amp; ;



*PREMATCH = *';



*POSTMATCH = *';



</FONT></PRE>



<P>Thus, when you use the English module in your program, you can use $MATCH, $PREMATCH,



and $POSTMATCH instead of using $&amp;, $', or $' and chasing through the manual



to verify whether you need an ampersand, backtick, or single quotation mark following



the $, each time you want to access these built-in variables. See the complete English.pm



module in @INC, and its embedded POD documentation, or English.3, the POD converted



to a manpage, for more details. <B><TT>New Logical Operators</TT></B> The new logical



operators and, or, and not enable you to avoid using the &amp;&amp;, ||, and unary



! operators, respectively. The new operators are definitely more readable, to the



casual observer at least. The former also have lower precedence than the comma, and



certain other low-precedence operators. Consider the following:</P>



<PRE><FONT COLOR="#0066FF">$foo/=0 || print &quot;aak&quot;;







# prints: aak



</FONT></PRE>



<P>Using ||, it looks like you can divide by 0. (Actually you're dividing by the



||'d value of zero and one, the return value from print.) Now consider the same example,



using the or operator:</P>



<PRE><FONT COLOR="#0066FF">$foo/=0 or print &quot;aak&quot;;







# prints: Illegal division by zero



</FONT></PRE>



<P>Using or (and Perl5) gives the expected result. See PERLOP for more details on



operator precedence. <B><TT>Warnings and Stricture</TT></B> Other usability enhancements



in Perl5 include improvements to the -w command-line switch, which now gives more



useful and informative output. You should use it with all your programs. See PERLRUN



for a complete description of all command-line switches.</P>



<P>Also new is a set of pragmatic modules, which impose certain restrictions and



perform extended type and syntax checking on your code at compile time, to potentially



help you find bugs before they bite. Use of these pragmas within your code is also



highly recommended. See PERLDSC and PERLMOD for more details on the motivation for



and impact of using the strict modules. <B><TT>The New </TT>=&gt;<TT> Operator</TT></B>



The =&gt; operator is syntactic sugar for a comma. It makes certain declarations



look prettier and appear more sensible, as in the following example:</P>



<PRE><FONT COLOR="#0066FF">%hash = (



    `Name' =&gt; `Joe',



    `Address' =&gt; `123 Foo Street',



    `City' =&gt; `San Francisco',



    `State' =&gt; `CA'



);



</FONT></PRE>



<P>Note how we used the comma at the end of each hash key/value pair, but the =&gt;



operator between them. This makes such declarations easier to read, especially when



declaring more complex data structures. <B><TT>Function Prototypes</TT></B> Function



prototypes are one of the very latest new features in Perl5. They were finally included



as of Perl5.002, after a great deal of consideration and discussion. Essentially,



they provide you with a means to assure that the correct arguments are passed to



your subroutines, to emulate the behavior of built-in commands, for instance. See



PERLSUB for more details.



<CENTER>



<H4><A NAME="Heading6"></A><FONT COLOR="#000077">Lexical Scoping</FONT></H4>



</CENTER>



<P>The new my() operator enables you to declare variables that are truly lexically



scoped. Previously, the closest you could get to a lexical variable was with local().



The my() variables are visible only within the current block (within a set of curly



braces) and go out of scope immediately after exiting the block. The following example



illustrates their use:</P>



<PRE><FONT COLOR="#0066FF">$foo = &quot;I\'m foo in a global context\n&quot;;



print $foo;







{  # the curlies open a new lexical context (block)



    my $foo = &quot;But I\'m bar in this short lexical context\n&quot;;



    print $foo;



} # lexically scoped $foo goes out of scope here







print $foo;







# prints:



I'm foo in a global context



But I'm bar in this short lexical context



I'm foo in a global context



</FONT></PRE>



<P>The my() variable is used extensively in the modules that you'll explore in this



book. See PERLVAR for complete details on all types of Perl variables, including



lexical variables.



<CENTER>



<H4><A NAME="Heading7"></A><FONT COLOR="#000077">References</FONT></H4>



</CENTER>



<P>Standard Perl scalars can now be used to refer to other data types. References



act much like pointers in C. In the general sense, they simply refer to other data



types. When a reference is blessed into a package, it becomes a Perl Object, and



can be used to invoke the methods of its package (or class), as well as to access



the instance variables of the class, something akin to C++ references.</P>



<P>Because we'll explore the references in depth later in this chapter, I'll defer



most of the examples, and a complete description of them until that time. I'll also



explain how the bless() operator works at that time. The reference datatype is also



used extensively in the examples in this book.



<CENTER>



<H4><A NAME="Heading8"></A><FONT COLOR="#000077">Data Structures</FONT></H4>



</CENTER>



<P>Nested data types and other data structures are now possible, using references.



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -