📄 appa.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Without a title - Title</TITLE>
</HEAD>
<BODY BACKGROUND="r2harch.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/r2harch.gif" TEXT="#000000" BGCOLOR="#FFFFFF">
<H2 ALIGN="CENTER"><A HREF="ch16.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch16.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42"
HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><BR>
<BR>
<FONT COLOR="#000077">Appendix A<BR>
<A NAME="Heading1"></A>Perl4 to Perl5 Traps<BR>
</FONT>
<HR>
</H2>
<UL>
<LI><A HREF="#Heading1">Perl4 to Perl5 Traps</A>
<UL>
<LI><A HREF="#Heading3">Discontinuance, Deprecation, and BugFix Traps</A>
<UL>
<LI><A HREF="#Heading4">Discontinuance</A>
<LI><A HREF="#Heading5">Deprecation</A>
<LI><A HREF="#Heading6">BugFix</A>
</UL>
<LI><A HREF="#Heading7">Parsing Traps</A>
<LI><A HREF="#Heading8">Numerical Traps</A>
<LI><A HREF="#Heading9">General Data Type Traps</A>
<UL>
<LI><A HREF="#Heading10">Scalar Arrays</A>
<LI><A HREF="#Heading11">Hashes</A>
<LI><A HREF="#Heading12">Globs</A>
<LI><A HREF="#Heading13">Scalar String</A>
<LI><A HREF="#Heading14">Constants</A>
<LI><A HREF="#Heading15">Scalars</A>
<LI><A HREF="#Heading16">Variable Suicide</A>
</UL>
<LI><A HREF="#Heading17">Context Traps--Scalar and List Contexts</A>
<UL>
<LI><A HREF="#Heading18">List Context</A>
<LI><A HREF="#Heading19">Scalar Context</A>
<LI><A HREF="#Heading20">List--Builtin</A>
</UL>
<LI><A HREF="#Heading21">Precedence Traps</A>
<LI><A HREF="#Heading22">General Regular Expression Traps Using s///</A>
<LI><A HREF="#Heading23">Subroutine, Signal, Sorting Traps</A>
<UL>
<LI><A HREF="#Heading24">Signal Handler Subroutines</A>
<LI><A HREF="#Heading25">Sort Subroutine</A>
<LI><A HREF="#Heading26">warn() Filehandle</A>
</UL>
<LI><A HREF="#Heading27">System V OS Traps</A>
<LI><A HREF="#Heading28">Interpolation Traps</A>
<LI><A HREF="#Heading29">DBM Traps</A>
<LI><A HREF="#Heading30">Unclassified Traps</A>
</UL>
</UL>
<P>
<HR>
</P>
<H2 ALIGN="CENTER"></H2>
<UL>
<LI>Discontinuance, Deprecation, and BugFix Traps
<LI>Parsing Traps
<LI>Numerical Traps
<LI>General Data Type Traps
<LI>Context Traps--Scalar and List Contexts
<LI>Precedence Traps
<LI>General Regular Expression Traps Using s///
<LI>Subroutine, Signal, Sorting Traps
<LI>System V OS Traps
<LI>Interpolation Traps
<LI>DBM Traps
<LI>Unclassified Traps
</UL>
<H3 ALIGN="CENTER">
<HR WIDTH="84%">
<BR>
<A NAME="Heading2"></A><FONT COLOR="#000077">NOTE:</FONT></H3>
<BLOCKQUOTE>
<P>Most of what follows in this appendix is taken directly from the PERLTRAP manual
section, which the author maintains. It's included here for your convenience in making
the transition from Perl4 to Perl5. The latest revision of this document is always
available within the Perl distribution.<BR>
<HR>
</BLOCKQUOTE>
<P>Practicing Perl4 programmers should take note of the following Perl4 to Perl5
conversion-<BR>
specific traps.</P>
<P>They're generally ordered according to the following list: Discontinuance, Deprecation,
Anything that's been fixed as a Perl4 bug, <BR>
and BugFix traps removed as a Perl4 feature, or deprecated as a Perl4 feature with
the intent to encourage usage of <BR>
some other Perl5 feature.</P>
<P>Parsing traps Traps that appear to stem from the new parser.</P>
<P>Numerical traps Traps having to do with numerical or mathematical operators.</P>
<P>General data type traps Traps involving Perl standard data types.</P>
<P>Context traps--scalar, Traps related to context within lists and scalar<BR>
list contexts statements/declarations.</P>
<P>Precedence Traps Traps related to the precedence of parsing, evaluation, and execution
of code.</P>
<P>General Regular Expression Traps related to the use of pattern matching.<BR>
traps using s///, and so on</P>
<P>Subroutine, Signal, Traps related to the use of signals and signal<BR>
Sorting traps handlers, general subroutines, and sorting,<BR>
along with sorting subroutines.</P>
<P>OS traps OS-specific traps.</P>
<P>DBM traps Traps specific to the use of dbmopen() and specific dbm implementations.
<BLOCKQUOTE>
<P>Unclassified traps Everything else.
</BLOCKQUOTE>
<P>If you find an example of a conversion trap that is not listed here, please submit
it to the Perl5 Porters <perl5-porters@nicoh.com> for inclusion. Also note
that at least some of these can be caught with the command-line option, -w.
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Discontinuance, Deprecation,
and BugFix Traps</FONT></H3>
<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>
<P>Anything that has been discontinued, deprecated, or fixed as a bug from Perl4.
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Discontinuance</FONT></H4>
<UL>
<LI>Symbols starting with "_" are no longer forced into package main, except
for $_ itself (@_, and so on).
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>package test;</P>
<P>$_legacy = 1;</P>
<P>package main;</P>
<P>print "\$_legacy is ",$_legacy,"\n";</P>
<P># perl4 prints: $_legacy is 1</P>
<P># perl5 prints: $_legacy is
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>You can't do a goto into a block that is optimized away. Darn.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>goto marker1;</P>
<P>for(1){</P>
<P>marker1:</P>
<P>print "Here I is!\n";</P>
<P>}</P>
<P># perl4 prints: Here I is!</P>
<P># perl5 dumps core (SEGV)
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>It is no longer syntactically legal to use whitespace as the name of a variable
or as a delimiter for any kind of quote construct. Double darn.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$a = ("foo bar");</P>
<P>$b = q baz ;</P>
<P>print "a is $a, b is $b\n";</P>
<P># perl4 prints: a is foo bar, b is baz</P>
<P># perl5 errors: Bare word found where operator expected
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<UL>
<UL>
<LI>m The archaic while/if BLOCK BLOCK syntax is no longer supported.
</UL>
</UL>
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>if { 1 } {</P>
<P>print "True!";</P>
<P>}</P>
<P>else {</P>
<P>print "False!";</P>
<P>}</P>
<P># perl4 prints: True!</P>
<P># perl5 errors: syntax error at test.pl line 1, near "if {"
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>The meaning of foreach{} has changed slightly when it is iterating over a list
which is not an array. This is used to assign the list to a temporary array, but
no longer does so (for efficiency). This means that you'll now be iterating over
the actual values, not over copies of the values. Modifications to the loop variable
can change the original values.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@list = (`ab','abc','bcd','def');</P>
<P>foreach $var (grep(/ab/,@list)){</P>
<P>$var = 1;</P>
<P>}</P>
<P>print (join(`:',@list));</P>
<P># perl4 prints: ab:abc:bcd:def</P>
<P># perl5 prints: 1:1:bcd:def
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>To retain Perl4 semantics, you need to assign your list explicitly to a temporary
array and then iterate over that. For example, you might need to change
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>foreach $var (grep(/ab/,@list)){
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>to
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>foreach $var (@tmp = grep(/ab/,@list)){
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Otherwise changing $var will clobber the values of @list. (This most often happens
when you use $_ for the loop variable, and call subroutines in the loop that don't
properly localize $_.)
<P>
<LI>split with no arguments now behaves like split(` `,$scalar), which doesn't return
an initial null field if $_ starts with whitespace. It used to behave like split(/\s+/,$scalar),
which does.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$_ = ' hi mom';</P>
<P>print join(`:', split);</P>
<P># perl4 prints: :hi:mom</P>
<P># perl5 prints: hi:mom
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Some bugs may have been inadvertently removed. :-)
</UL>
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Deprecation</FONT></H4>
<UL>
<LI>Double-colon is now a valid package separator in a variable name. Thus, these
behave differently in Perl4 versus Perl5, since the packages don't exist.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$a=1;$b=2;$c=3;$var=4;</P>
<P>print "$a::$b::$c ";</P>
<P>print "$var::abc::xyz\n";</P>
<P># perl4 prints: 1::2::3 4::abc::xyz</P>
<P># perl5 prints: 3
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Given that :: is now the preferred package delimiter, it is debatable whether
this should be classed as a bug or not. (The older package delimiter, ` ,is used
here)
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$x = 10 ;</P>
<P>print "x=${`x}\n" ;</P>
<P># perl4 prints: x=10</P>
<P># perl5 prints: Can't find string terminator "'" anywhere before EOF
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Also see precedence traps for parsing $:.
<P>
<LI>Some error messages will be different.
</UL>
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">BugFix</FONT></H4>
<UL>
<LI>The second and third arguments of splice() are now evaluated in scalar context
(as the Camel says) rather than list context.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>sub sub1{return(0,2) } # return a 2-elem array</P>
<P>sub sub2{ return(1,2,3)} # return a 3-elem array</P>
<P>@a1 = ("a","b","c","d","e");</P>
<P>@a2 = splice(@a1,&sub1,&sub2);</P>
<P>print join(` ',@a2),"\n";</P>
<P># perl4 prints: a b</P>
<P># perl5 prints: c d e
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>The ** operator now binds more tightly than unary minus. It was documented to
work this way before but didn't.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>print -4**2,"\n";</P>
<P># perl4 prints: 16</P>
<P># perl5 prints: -16
</BLOCKQUOTE>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">Parsing Traps</FONT></H3>
<P>Perl4 to Perl5 traps having to do with parsing.
<UL>
<LI>Note the space between . and =.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$string . = "more string";</P>
<P>print $string;</P>
<P># perl4 prints: more string</P>
<P># perl5 prints: syntax error at - line 1, near ". ="
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Better parsing in Perl5.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>sub foo {}</P>
<P>&foo</P>
<P>print("hello, world\n");</P>
<P># perl4 prints: hello, world</P>
<P># perl5 prints: syntax error
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>"If it looks like a function, it is a function" rule.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>print</P>
<P>($foo == 1) ? "is one\n" : "is zero\n";</P>
<P># perl4 prints: is zero</P>
<P># perl5 warns: "Useless use of a constant in void context" if using
-w
</BLOCKQUOTE>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">Numerical Traps</FONT></H3>
<P>Perl4 to Perl5 traps having to do with numerical operators, operands, or output
from same.
<UL>
<LI>Formatted output and significant digits.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>print 7.373504 - 0, "\n";</P>
<P>printf "%20.18f\n", 7.373504 - 0;</P>
<P># Perl4 prints:</P>
<P>7.375039999999996141</P>
<P>7.37503999999999614</P>
<P># Perl5 prints:</P>
<P>7.373504</P>
<P>7.37503999999999614
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Assignment of return values from numeric equality tests does not work in Perl5
when the test evaluates to false (0). Logical tests now return a null, instead of
0.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$p = ($test == 1); print $p,"\n";</P>
<P># perl4 prints: 0</P>
<P># perl5 prints:
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Also see the "General Regular Expression Traps" section for another
example of this new feature.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">General Data Type
Traps</FONT></H3>
<P>Perl4 to Perl5 traps involving most data types and their usage within certain
expressions and/or context.
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Scalar Arrays</FONT></H4>
<UL>
<LI>Negative array subscripts now count from the end of the array.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@a = (1, 2, 3, 4, 5);</P>
<P>print "The third element of the array is $a[3] also expressed as $a[-2] \n";</P>
<P># perl4 prints: The third element of the array is 4 also expressed as</P>
<P># perl5 prints: The third element of the array is 4 also expressed as 4
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Setting $#array lower now discards array elements and makes them impossible to
recover.
</UL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -