📄 appa.htm
字号:
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@a = (a,b,c,d,e);</P>
<P>print "Before: ",join(``,@a);</P>
<P>$#a =1;</P>
<P>print ", After: ",join(``,@a);</P>
<P>$#a =3;</P>
<P>print ", Recovered: ",join(``,@a),"\n";</P>
<P># perl4 prints: Before: abcde, After: ab, Recovered: abcd</P>
<P># perl5 prints: Before: abcde, After: ab, Recovered: ab
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Hashes</FONT></H4>
<UL>
<LI>Hashes get defined before use.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>local($s,@a,%h);</P>
<P>die "scalar \$s defined" if defined($s);</P>
<P>die "array \@a defined" if defined(@a);</P>
<P>die "hash \%h defined" if defined(%h);</P>
<P># perl4 prints:</P>
<P># perl5 dies: hash %h defined
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Globs</FONT></H4>
<UL>
<LI>glob assignment from variable to variable will fail if the assigned variable
is localized subsequent to the assignment.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@a = ("This is Perl 4");</P>
<P>*b = *a;</P>
<P>local(@a);</P>
<P>print @b,"\n";</P>
<P># perl4 prints: This is Perl 4</P>
<P># perl5 prints:</P>
<P># Another example</P>
<P>*fred = *barney; # fred is aliased to barney</P>
<P>@barney = (1, 2, 4);</P>
<P># @fred;</P>
<P>print "@fred"; # should print "1, 2, 4"</P>
<P># perl4 prints: 1 2 4</P>
<P># perl5 prints: Literal @fred now requires backslash
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Scalar String</FONT></H4>
<H4 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H4>
<UL>
<LI>Changes in unary negation (of strings). This change affects both the return value
and what it does to auto (magic) increment.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$x = "aaa";</P>
<P>print ++$x," : ";</P>
<P>print -$x," : ";</P>
<P>print ++$x,"\n";</P>
<P># perl4 prints: aab : -0 : 1</P>
<P># perl5 prints: aab : -aab : aac
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Constants</FONT></H4>
<H4 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H4>
<UL>
<LI>Perl4 lets you modify constants:
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$foo = "x";</P>
<P>&mod($foo);</P>
<P>for ($x = 0; $x < 3; $x++) {</P>
<P>&mod("a");</P>
<P>}</P>
<P>sub mod {</P>
<P>print "before: $_[0]";</P>
<P>$_[0] = "m";</P>
<P>print " after: $_[0]\n";</P>
<P>}</P>
<P># perl4:</P>
<P># before: x after: m</P>
<P># before: a after: m</P>
<P># before: m after: m</P>
<P># before: m after: m</P>
<P># Perl5:</P>
<P># before: x after: m</P>
<P># Modification of a read-only value attempted at foo.pl line 12.</P>
<P># before: a
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">Scalars</FONT></H4>
<H4 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H4>
<UL>
<LI>The behavior is slightly different for
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>print "$x", defined $x</P>
<P># perl 4: 1</P>
<P># perl 5: <no output, $x is not called into existence>
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Variable Suicide</FONT></H4>
<H4 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H4>
<UL>
<LI>Variable suicide behavior is more consistent under Perl5. Perl5 exhibits the
same behavior for associative arrays and scalars that Perl4 exhibits only for scalars.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$aGlobal{ "aKey" } = "global value";</P>
<P>print "MAIN:", $aGlobal{"aKey"}, "\n";</P>
<P>$GlobalLevel = 0;</P>
<P>&test( *aGlobal );</P>
<P>sub test {</P>
<P>local( *theArgument ) = @_;</P>
<P>local( %aNewLocal ); # perl 4 != 5.001l,m</P>
<P>$aNewLocal{"aKey"} = "this should never appear";</P>
<P>print "SUB: ", $theArgument{"aKey"}, "\n";</P>
<P>$aNewLocal{"aKey"} = "level $GlobalLevel"; # what should print</P>
<P>$GlobalLevel++;</P>
<P>if( $GlobalLevel<4 ) {</P>
<P>&test( *aNewLocal );</P>
<P>}</P>
<P>}</P>
<P># Perl4:</P>
<P># MAIN:global value</P>
<P># SUB: global value</P>
<P># SUB: level 0</P>
<P># SUB: level 1</P>
<P># SUB: level 2</P>
<P># Perl5:</P>
<P># MAIN:global value</P>
<P># SUB: global value</P>
<P># SUB: this should never appear</P>
<P># SUB: this should never appear</P>
<P># SUB: this should never appear
</BLOCKQUOTE>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Context Traps--Scalar
and List Contexts</FONT></H3>
<P>Traps that involve scalar versus list contexts.
<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">List Context</FONT></H4>
<UL>
<LI>The elements of argument lists for formats are now evaluated in list context.
This means you can interpolate list values now.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@fmt = ("foo","bar","baz");</P>
<P>format STDOUT=</P>
<P>@<<<<< @||||| @>>>>></P>
<P>@fmt;</P>
<P>.</P>
<P>write;</P>
<P># perl4 errors: "Please use commas to separate fields in file" if using
-w</P>
<P># perl5 prints: foo bar baz
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Scalar Context</FONT></H4>
<UL>
<LI>The caller()function now returns a false value in a scalar context if there is
no caller. This lets library files determine if they're being required.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>caller() ? (print "You rang?\n") : (print "Got a 0\n");</P>
<P># perl4 errors: There is no caller</P>
<P># perl5 prints: Got a 0
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>The comma operator in a scalar context is now guaranteed to give a scalar context
to its arguments.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@y= (`a','b','c');</P>
<P>$x = (1, 2, @y);</P>
<P>print "x = $x\n";</P>
<P># Perl4 prints: x = c # Thinks list context interpolates list</P>
<P># Perl5 prints: x = 3 # Knows scalar uses length of list
</BLOCKQUOTE>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">List--Builtin</FONT></H4>
<UL>
<LI>sprintf() funkiness (array argument converted to scalar array count). This test
could be added to the t/op/sprintf.t tests.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@z = (`%s%s', 'foo', 'bar');</P>
<P>$x = sprintf(@z);</P>
<P>if ($x eq 'foobar') {print "ok 2\n";} else {print "not ok 2 '$x'\n";}</P>
<P># perl4 prints: ok 2</P>
<P># perl5 prints: not ok 2</P>
<P>printf() works fine, though:</P>
<P>printf STDOUT (@z);</P>
<P>print "\n";</P>
<P># perl4 prints: foobar</P>
<P># perl5 prints: foobar
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Probably a bug.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Precedence Traps</FONT></H3>
<P>Perl4 to Perl5 traps involving precedence of operations and ordering.
<UL>
<LI>LHS versus RHS when both sides are getting an op.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@arr = ( 'left', 'right' );</P>
<P>$a{shift @arr} = shift @arr;</P>
<P>print join( ' ', keys %a );</P>
<P># perl4 prints: left</P>
<P># perl5 prints: right
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>These are now semantic errors because of precedence.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@list = (1,2,3,4,5);</P>
<P>%map = ("a",1,"b",2,"c",3,"d",4);</P>
<P>$n = shift @list + 2; # first item in list plus 2</P>
<P>print "n is $n, ";</P>
<P>$m = keys %map + 2; # number of items in hash plus 2</P>
<P>print "m is $m\n";</P>
<P># perl4 prints: n is 3, m is 6</P>
<P># perl5 errors and fails to compile
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>The precedence of assignment operators is now the same as the precedence of assignment.
Perl4 mistakenly gave them the precedence of the associated operator. So, you now
must parenthesize them in expressions like
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>/foo/ ? ($a += 2) : ($a -= 2);
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Otherwise,
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>/foo/ ? $a += 2 : $a -= 2
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>would be erroneously parsed as
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>(/foo/ ? $a += 2 : $a) -= 2;
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>On the other hand,
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$a += /foo/ ? 1 : 2;
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>now works as a C programmer would expect.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>open FOO || die;
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>is now incorrect. You need parens around the filehandle. Otherwise, Perl5 leaves
the statement as its default precedence:
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>open(FOO || die);</P>
<P># perl4 opens or dies</P>
<P># perl5 errors: Precedence problem: open FOO should be open(FOO)
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Perl4 gives the special variable, $: precedence, where Perl5 treats $:: as main::
package.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$a = "x"; print "$::a";</P>
<P># perl 4 prints: -:a</P>
<P># perl 5 prints: x
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Concatenation precedence over the filetest operator?
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>-e $foo .= "q"</P>
<P># perl4 prints: no output</P>
<P># perl5 prints: Can't modify -e in concatenation
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Assignment to value takes precedence over assignment to key in Perl5 when using
the shift operator on both sides.
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>@arr = ( 'left', 'right' );</P>
<P>$a{shift @arr} = shift @arr;</P>
<P>print join( ' ', keys %a );</P>
<P># perl4 prints: left</P>
<P># perl5 prints: right
</BLOCKQUOTE>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">General Regular
Expression Traps Using s///</FONT></H3>
<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>
<P>All types of regular expression and regular expression operator traps.
<UL>
<LI>s'$lhs'$rhs' now does no interpolation on either side. It used to interpolate
$lhs but not $rhs (and still does not match a literal `$' in the string).
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$a=1;$b=2;</P>
<P>$string = '1 2 $a $b';</P>
<P>$string =~ s'$a'$b';</P>
<P>print $string,"\n";</P>
<P># perl4 prints: $b 2 $a $b</P>
<P># perl5 prints: 1 2 $a $b
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>m//g now attaches its state to the searched string rather than the regular expression.
(Once the scope of a block is left for the sub, the state of the searched string
is lost.)
</UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>$_ = "ababab";</P>
<P>while(m/ab/g){</P>
<P>&doit("blah");</P>
<P>}</P>
<P>sub doit{local($_) = shift; print "Got $_ "}</P>
<P># perl4 prints: blah blah blah</P>
<P># perl5 prints: infinite loop blah...
</BLOCKQUOTE>
</BLOCKQUOTE>
<UL>
<LI>Substitution now returns the null string if it fails.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -