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

📄 appa.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$string = &quot;test&quot;;</P>



		<P>$value = ($string =~ s/foo//);</P>



		<P>print $value, &quot;\n&quot;;</P>



		<P># perl4 prints: 0</P>



		<P># perl5 prints:







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Also see the section &quot;Numerical Traps&quot; for another example of this



	new feature.



	<LI>s'lhs'rhs' (using backticks) is now a normal substitution, with no backtick expansion.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$string = &quot;&quot;;</P>



		<P>$string =~ s'^'hostname';</P>



		<P>print $string, &quot;\n&quot;;</P>



		<P># perl4 prints: &lt;the local hostname&gt;</P>



		<P># perl5 prints: hostname







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Stricter parsing of variables is used in regular expressions.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;</P>



		<P># perl4: compiles w/o error</P>



		<P># perl5: with Scalar found where operator expected ..., near &quot;$opt$plus&quot;







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>An added component of this example, apparently from the same script, is the actual



	value of the s'd string after the substitution. [$opt] is a character class in Perl4



	and an array subscript in Perl5.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$grpc = 'a';</P>



		<P>$opt = 'r';</P>



		<P>$_ = 'bar';</P>



		<P>s/^([^$grpc]*$grpc[$opt]?)/foo/;</P>



		<P>print ;</P>



		<P># perl4 prints: foo</P>



		<P># perl5 prints: foobar







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Under Perl5, m?x? matches only once, like ?x?. Under Perl4, it matched repeatedly,



	like <BR>



	/x/ or m!x!.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$test = &quot;once&quot;;</P>



		<P>sub match { $test =~ m?once?; }</P>



		<P>&amp;match();</P>



		<P>if( &amp;match() ) {</P>



		<P># m?x? matches more then once</P>



		<P>print &quot;perl4\n&quot;;</P>



		<P>} else {</P>



		<P># m?x? matches only once</P>



		<P>print &quot;perl5\n&quot;;</P>



		<P>}</P>



		<P># perl4 prints: perl4</P>



		<P># perl5 prints: perl5







	</BLOCKQUOTE>







</BLOCKQUOTE>







<H3 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Subroutine, Signal,



Sorting Traps</FONT></H3>



<P>The general group of Perl4 to Perl5 traps having to do with signals, sorting,



and their related subroutines, as well as general subroutine traps. Includes some



OS-specific traps.



<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Signal Handler Subroutines</FONT></H4>







<UL>



	<LI>Barewords that used to look like strings to Perl will now look like subroutine



	calls if a subroutine by that name is defined before the compiler sees them.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>sub SeeYa { warn&quot;Hasta la vista, baby!&quot; }</P>



		<P>$SIG{`TERM'} = SeeYa;</P>



		<P>print &quot;SIGTERM is now $SIG{`TERM'}\n&quot;;</P>



		<P># perl4 prints: SIGTERM is main'SeeYa</P>



		<P># perl5 prints: SIGTERM is now main::1







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Use -w to catch this one.



</UL>







<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Sort Subroutine</FONT></H4>







<UL>



	<LI>reverse is no longer allowed as the name of a sort subroutine.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>sub reverse{ print &quot;yup &quot;; $a &lt;=&gt; $b }</P>



		<P>print sort reverse a,b,c;</P>



		<P># perl4 prints: yup yup yup yup abc</P>



		<P># perl5 prints: abc







	</BLOCKQUOTE>







</BLOCKQUOTE>







<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">warn() Filehandle</FONT></H4>



<H4 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H4>







<UL>



	<LI>Although it always printed to STDERR, warn() would let you specify a filehandle



	in Perl4. With Perl5 it does not.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>warn STDERR &quot;Foo!&quot;;</P>



		<P># perl4 prints: Foo!</P>



		<P># perl5 prints: String found where operator expected







	</BLOCKQUOTE>







</BLOCKQUOTE>







<H3 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">System V OS Traps</FONT></H3>



<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>







<UL>



	<LI>Under HPUX, and some other System V Operating Systems, one had to reset any signal



	handler, within the signal handler function, each time a signal was handled when



	using Perl4. With Perl5, the reset is now done correctly. Any code relying on the



	handler not being reset will have to be reworked.



	<P>



	<LI>Perl5.002 and later uses sigaction() to handle signals under System V.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>sub gotit {</P>



		<P>print &quot;Got @_... &quot;;</P>



		<P>}</P>



		<P>$SIG{`INT'} = 'gotit';</P>



		<P>$| = 1;</P>



		<P>$pid = fork;</P>



		<P>if ($pid) {</P>



		<P>kill(`INT', $pid);</P>



		<P>sleep(1);</P>



		<P>kill(`INT', $pid);</P>



		<P>} else {</P>



		<P>while (1) {sleep(10);}</P>



		<P>}</P>



		<P># perl4 (HPUX) prints: Got INT...</P>



		<P># perl5 (HPUX) prints: Got INT... Got INT...







	</BLOCKQUOTE>







</BLOCKQUOTE>







<H3 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Interpolation Traps</FONT></H3>



<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>



<P>Perl4 to Perl5 traps having to do with how things get interpolated within certain



expressions, statements, contexts, or whatever.







<UL>



	<LI>@ now always interpolates an array in double-quotish strings.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>print &quot;To: someone@somewhere.com\n&quot;;</P>



		<P># perl4 prints: To:someone@somewhere.com</P>



		<P># perl5 errors : Literal @somewhere now requires backslash







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Double-quoted strings may no longer end with an unescaped $ or @.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$foo = &quot;foo$&quot;;</P>



		<P>$bar = &quot;bar@&quot;;</P>



		<P>print &quot;foo is $foo, bar is $bar\n&quot;;</P>



		<P># perl4 prints: foo is foo$, bar is bar@</P>



		<P># perl5 errors: Final $ should be \$ or $name







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Note: Perl5 does not error on the terminating @ in $bar.



	<P>



	<LI>m Perl now sometimes evaluates arbitrary expressions inside braces that occur



	within double quotes (usually when the opening brace is preceded by $ or @).



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>@www = &quot;buz&quot;;</P>



		<P>$foo = &quot;foo&quot;;</P>



		<P>$bar = &quot;bar&quot;;</P>



		<P>sub foo { return &quot;bar&quot; };</P>



		<P>print &quot;|@{w.w.w}|${main'foo}|&quot;;</P>



		<P># perl4 prints: |@{w.w.w}|foo|</P>



		<P># perl5 prints: |buz|bar|







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Note that you can use strict; to ward off such trappiness under Perl5.



	<P>



	<LI>The quoted string &quot;this is $$x&quot; used to interpolate the pid at that



	point but now tries to dereference $x. $$ by itself still works fine.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>print &quot;this is $$x\n&quot;;</P>



		<P># perl4 prints: this is XXXx (XXX is the current pid)</P>



		<P># perl5 prints: this is







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Creation of hashes on the fly with eval &quot;EXPR&quot; now requires either



	both $s to be protected in the specification of the hash name, or both curlies to



	be protected. If both curlies are protected, the result will be compatible with Perl4



	and Perl5. This is a very common practice and should be changed to use the block



	form of eval{} if possible.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$hashname = &quot;foobar&quot;;</P>



		<P>$key = &quot;baz&quot;;</P>



		<P>$value = 1234;</P>



		<P>eval &quot;\$$hashname{`$key'} = q|$value|&quot;;</P>



		<P>(defined($foobar{`baz'})) ? (print &quot;Yup&quot;) : (print &quot;Nope&quot;);</P>



		<P># perl4 prints: Yup</P>



		<P># perl5 prints: Nope</P>



		<P>Changing</P>



		<P>eval &quot;\$$hashname{`$key'} = q|$value|&quot;;</P>



		<P>to</P>



		<P>eval &quot;\$\$hashname{`$key'} = q|$value|&quot;;</P>



		<P>causes the following result:</P>



		<P># perl4 prints: Nope</P>



		<P># perl5 prints: Yup</P>



		<P>or, changing to</P>



		<P>eval &quot;\$$hashname\{`$key'\} = q|$value|&quot;;</P>



		<P>causes the following result:</P>



		<P># perl4 prints: Yup</P>



		<P># perl5 prints: Yup</P>



		<P># and is compatible for both versions







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Watch out forPerl4 programs which unconsciously rely on the bugs in earlier Perl



	versions.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>perl -e '$bar=q/not/; print &quot;This is $foo{$bar} perl5&quot;'</P>



		<P># perl4 prints: This is not perl5</P>



		<P># perl5 prints: This is perl5







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>You also have to be careful about array references.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>print &quot;$foo{&quot;</P>



		<P>perl 4 prints: {</P>



		<P>perl 5 prints: syntax error







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Similarly, watch out for



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>$foo = &quot;array&quot;;</P>



		<P>print &quot;\$$foo{bar}\n&quot;;</P>



		<P># perl4 prints: $array{bar}</P>



		<P># perl5 prints: $







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Perl5 is looking for $array{bar} which doesn't exist, but Perl4 is happy just



	to expand $foo to &quot;array&quot; by itself. Watch out for this especially in eval()s.



	<P>



	<LI>m qq() string passed to eval.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>eval qq(</P>



		<P>foreach \$y (keys %\$x\) {</P>



		<P>\$count++;</P>



		<P>}</P>



		<P>);</P>



		<P># perl4 runs this ok</P>



		<P># perl5 prints: Can't find string terminator &quot;)&quot;







	</BLOCKQUOTE>







</BLOCKQUOTE>







<H3 ALIGN="CENTER"><A NAME="Heading29"></A><FONT COLOR="#000077">DBM Traps</FONT></H3>



<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>



<P>General DBM traps.







<UL>



	<LI>Existing dbm databases created under Perl4 (or any other dbm/ndbm tool) may cause



	the same script, run under Perl5, to fail. The build of Perl5 must have been linked



	with the same dbm/ndbm as the default for dbmopen() to function properly without



	tieing to an extension dbm implementation.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>dbmopen (%dbm, &quot;file&quot;, undef);</P>



		<P>print &quot;ok\n&quot;;</P>



		<P># perl4 prints: ok</P>



		<P># perl5 prints: ok (IFF linked with -ldbm or -lndbm)







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>Existing dbm databases created under Perl4 (or any other dbm/ndbm tool) may cause



	the same script, run under Perl5, to fail. The error generated when exceeding the



	limit on the key/value size will cause Perl5 to exit immediately.



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>dbmopen(DB, &quot;testdb&quot;,0600) || die &quot;couldn't open db! $!&quot;;</P>



		<P>$DB{`trap'} = &quot;x&quot; x 1024; # value too large for most dbm/ndbm</P>



		<P>print &quot;YUP\n&quot;;</P>



		<P># perl4 prints:</P>



		<P>dbm store returned -1, errno 28, key &quot;trap&quot; at - line 3.</P>



		<P>YUP</P>



		<P># perl5 prints:</P>



		<P>dbm store returned -1, errno 28, key &quot;trap&quot; at - line 3.







	</BLOCKQUOTE>







</BLOCKQUOTE>







<H3 ALIGN="CENTER"><A NAME="Heading30"></A><FONT COLOR="#000077">Unclassified Traps</FONT></H3>



<H2 ALIGN="CENTER"></H2>



<H3 ALIGN="CENTER"><FONT COLOR="#000077"><BR>



<BR>



</FONT></H3>



<P>Everything else.







<UL>



	<LI>require/do trap using returned value.



	<LI>If the file doit.pl has



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>sub foo {</P>



		<P>$rc = do &quot;./do.pl&quot;;</P>



		<P>return 8;</P>



		<P>}</P>



		<P>print &amp;foo, &quot;\n&quot;;







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>and the do.pl file has the following single line



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P>return 3;







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>running doit.pl gives the following



</UL>















<BLOCKQUOTE>







	<BLOCKQUOTE>



		<P># perl 4 prints: 3 (aborts the subroutine early)</P>



		<P># perl 5 prints: 8







	</BLOCKQUOTE>







</BLOCKQUOTE>











<UL>



	<LI>The same behavior occurs if you replace do with require.



	<P>



	<LI>As always, if any of these are ever officially declared as bugs, they'll be fixed



	and removed.<FONT COLOR="#000077"></FONT>



</UL>







<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>











</BODY>







</HTML>

⌨️ 快捷键说明

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