📄 perltrap.1
字号:
\& $string =~ s\`^\`hostname\`;\& print $string, "\en";\&\& # perl4 prints: <the local hostname>\& # perl5 prints: hostname.Ve.IP "\(bu" 5Stricter parsing of variables in regular expressions.SpStricter parsing of variables used in regular expressions.Sp.Vb 1\& s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;\&\& # perl4: compiles w/o error\& # perl5: with Scalar found where operator expected ..., near "$opt$plus".Ve.Span added component of this example, apparently from the same script, isthe actual value of the s'd string after the substitution.\&\f(CW\*(C`[$opt]\*(C'\fR is a character class in perl4 and an array subscript in perl5.Sp.Vb 5\& $grpc = \*(Aqa\*(Aq;\& $opt = \*(Aqr\*(Aq;\& $_ = \*(Aqbar\*(Aq;\& s/^([^$grpc]*$grpc[$opt]?)/foo/;\& print;\&\& # perl4 prints: foo\& # perl5 prints: foobar.Ve.IP "\(bu" 5\&\f(CW\*(C`m?x?\*(C'\fR matches only once.SpUnder perl5, \f(CW\*(C`m?x?\*(C'\fR matches only once, like \f(CW\*(C`?x?\*(C'\fR. Under perl4, it matchedrepeatedly, like \f(CW\*(C`/x/\*(C'\fR or \f(CW\*(C`m!x!\*(C'\fR..Sp.Vb 10\& $test = "once";\& sub match { $test =~ m?once?; }\& &match();\& if( &match() ) {\& # m?x? matches more then once\& print "perl4\en";\& } else {\& # m?x? matches only once\& print "perl5\en";\& }\&\& # perl4 prints: perl4\& # perl5 prints: perl5.Ve.IP "\(bu" 5Failed matches don't reset the match variables.SpUnlike in Ruby, failed matches in Perl do not reset the match variables($1, \f(CW$2\fR, ..., \f(CW\*(C`$\`\*(C'\fR, ...)..Sh "Subroutine, Signal, Sorting Traps".IX Subsection "Subroutine, Signal, Sorting Traps"The general group of Perl4\-to\-Perl5 traps having to do withSignals, Sorting, and their related subroutines, as well asgeneral subroutine traps. Includes some OS-Specific traps..IP "\(bu" 5Barewords that used to look like strings look like subroutine calls.SpBarewords that used to look like strings to Perl will now look like subroutinecalls if a subroutine by that name is defined before the compiler sees them..Sp.Vb 3\& sub SeeYa { warn"Hasta la vista, baby!" }\& $SIG{\*(AqTERM\*(Aq} = SeeYa;\& print "SIGTERM is now $SIG{\*(AqTERM\*(Aq}\en";\&\& # perl4 prints: SIGTERM is now main\*(AqSeeYa\& # perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!").Ve.SpUse \fB\-w\fR to catch this one.IP "\(bu" 5Reverse is no longer allowed as the name of a sort subroutine.Spreverse is no longer allowed as the name of a sort subroutine..Sp.Vb 2\& sub reverse{ print "yup "; $a <=> $b }\& print sort reverse (2,1,3);\&\& # perl4 prints: yup yup 123\& # perl5 prints: 123\& # perl5 warns (if using \-w): Ambiguous call resolved as CORE::reverse().Ve.IP "\(bu" 5\&\f(CW\*(C`warn()\*(C'\fR won't let you specify a filehandle..SpAlthough it _always_ printed to \s-1STDERR\s0, \fIwarn()\fR would let you specify afilehandle in perl4. With perl5 it does not..Sp.Vb 1\& warn STDERR "Foo!";\&\& # perl4 prints: Foo!\& # perl5 prints: String found where operator expected.Ve.Sh "\s-1OS\s0 Traps".IX Subsection "OS Traps".IP "\(bu" 5SysV resets signal handler correctly.SpUnder \s-1HPUX\s0, and some other SysV OSes, one had to reset any signal handler,within the signal handler function, each time a signal was handled withperl4. With perl5, the reset is now done correctly. Any code relyingon the handler _not_ being reset will have to be reworked..SpSince version 5.002, Perl uses \fIsigaction()\fR under SysV..Sp.Vb 4\& sub gotit {\& print "Got @_... ";\& }\& $SIG{\*(AqINT\*(Aq} = \*(Aqgotit\*(Aq;\&\& $| = 1;\& $pid = fork;\& if ($pid) {\& kill(\*(AqINT\*(Aq, $pid);\& sleep(1);\& kill(\*(AqINT\*(Aq, $pid);\& } else {\& while (1) {sleep(10);}\& }\&\& # perl4 (HPUX) prints: Got INT...\& # perl5 (HPUX) prints: Got INT... Got INT....Ve.IP "\(bu" 5SysV \f(CW\*(C`seek()\*(C'\fR appends correctly.SpUnder SysV OSes, \f(CW\*(C`seek()\*(C'\fR on a file opened to append \f(CW\*(C`>>\*(C'\fR now doesthe right thing w.r.t. the \fIfopen()\fR manpage. e.g., \- When a file is openedfor append, it is impossible to overwrite information already inthe file..Sp.Vb 8\& open(TEST,">>seek.test");\& $start = tell TEST;\& foreach(1 .. 9){\& print TEST "$_ ";\& }\& $end = tell TEST;\& seek(TEST,$start,0);\& print TEST "18 characters here";\&\& # perl4 (solaris) seek.test has: 18 characters here\& # perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here.Ve.Sh "Interpolation Traps".IX Subsection "Interpolation Traps"Perl4\-to\-Perl5 traps having to do with how things get interpolatedwithin certain expressions, statements, contexts, or whatever..IP "\(bu" 5\&\f(CW\*(C`@\*(C'\fR always interpolates an array in double-quotish strings.Sp@ now always interpolates an array in double-quotish strings..Sp.Vb 1\& print "To: someone@somewhere.com\en";\&\& # perl4 prints: To:someone@somewhere.com\& # perl < 5.6.1, error : In string, @somewhere now must be written as \e@somewhere\& # perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string.Ve.IP "\(bu" 5Double-quoted strings may no longer end with an unescaped $.SpDouble-quoted strings may no longer end with an unescaped $..Sp.Vb 2\& $foo = "foo$";\& print "foo is $foo\en";\&\& # perl4 prints: foo is foo$\& # perl5 errors: Final $ should be \e$ or $name.Ve.SpNote: perl5 \s-1DOES\s0 \s-1NOT\s0 error on the terminating @ in \f(CW$bar\fR.IP "\(bu" 5Arbitrary expressions are evaluated inside braces within double quotes.SpPerl now sometimes evaluates arbitrary expressions inside braces that occurwithin double quotes (usually when the opening brace is preceded by \f(CW\*(C`$\*(C'\fRor \f(CW\*(C`@\*(C'\fR)..Sp.Vb 5\& @www = "buz";\& $foo = "foo";\& $bar = "bar";\& sub foo { return "bar" };\& print "|@{w.w.w}|${main\*(Aqfoo}|";\&\& # perl4 prints: |@{w.w.w}|foo|\& # perl5 prints: |buz|bar|.Ve.SpNote that you can \f(CW\*(C`use strict;\*(C'\fR to ward off such trappiness under perl5..IP "\(bu" 5\&\f(CW$$x\fR now tries to dereference \f(CW$x\fR.SpThe construct \*(L"this is $$x\*(R" used to interpolate the pid at that point, butnow tries to dereference \f(CW$x\fR. \f(CW$$\fR by itself still works fine, however..Sp.Vb 3\& $s = "a reference";\& $x = *s;\& print "this is $$x\en";\&\& # perl4 prints: this is XXXx (XXX is the current pid)\& # perl5 prints: this is a reference.Ve.IP "\(bu" 5Creation of hashes on the fly with \f(CW\*(C`eval "EXPR"\*(C'\fR requires protection.SpCreation of hashes on the fly with \f(CW\*(C`eval "EXPR"\*(C'\fR now requires either both\&\f(CW\*(C`$\*(C'\fR's to be protected in the specification of the hash name, or both curliesto be protected. If both curlies are protected, the result will be compatiblewith perl4 and perl5. This is a very common practice, and should be changedto use the block form of \f(CW\*(C`eval{}\*(C'\fR if possible..Sp.Vb 5\& $hashname = "foobar";\& $key = "baz";\& $value = 1234;\& eval "\e$$hashname{\*(Aq$key\*(Aq} = q|$value|";\& (defined($foobar{\*(Aqbaz\*(Aq})) ? (print "Yup") : (print "Nope");\&\& # perl4 prints: Yup\& # perl5 prints: Nope.Ve.SpChanging.Sp.Vb 1\& eval "\e$$hashname{\*(Aq$key\*(Aq} = q|$value|";.Ve.Spto.Sp.Vb 1\& eval "\e$\e$hashname{\*(Aq$key\*(Aq} = q|$value|";.Ve.Spcauses the following result:.Sp.Vb 2\& # perl4 prints: Nope\& # perl5 prints: Yup.Ve.Spor, changing to.Sp.Vb 1\& eval "\e$$hashname\e{\*(Aq$key\*(Aq\e} = q|$value|";.Ve.Spcauses the following result:.Sp.Vb 3\& # perl4 prints: Yup\& # perl5 prints: Yup\& # and is compatible for both versions.Ve.IP "\(bu" 5Bugs in earlier perl versions.Spperl4 programs which unconsciously rely on the bugs in earlier perl versions..Sp.Vb 1\& perl \-e \*(Aq$bar=q/not/; print "This is $foo{$bar} perl5"\*(Aq\&\& # perl4 prints: This is not perl5\& # perl5 prints: This is perl5.Ve.IP "\(bu" 5Array and hash brackets during interpolation.SpYou also have to be careful about array and hash brackets duringinterpolation..Sp.Vb 1\& print "$foo["\&\& perl 4 prints: [\& perl 5 prints: syntax error\&\& print "$foo{"\&\& perl 4 prints: {\& perl 5 prints: syntax error.Ve.SpPerl 5 is expecting to find an index or key name following the respectivebrackets, as well as an ending bracket of the appropriate type. In orderto mimic the behavior of Perl 4, you must escape the bracket like so..Sp.Vb 2\& print "$foo\e[";\& print "$foo\e{";.Ve.IP "\(bu" 5Interpolation of \f(CW\*(C`\e$$foo{bar}\*(C'\fR.SpSimilarly, watch out for: \f(CW\*(C`\e$$foo{bar}\*(C'\fR.Sp.Vb 2\& $foo = "baz";\& print "\e$$foo{bar}\en";\&\& # perl4 prints: $baz{bar}\& # perl5 prints: $.Ve.SpPerl 5 is looking for \f(CW$foo{bar}\fR which doesn't exist, but perl 4 ishappy just to expand \f(CW$foo\fR to \*(L"baz\*(R" by itself. Watch out for thisespecially in \f(CW\*(C`eval\*(C'\fR's..IP "\(bu" 5\&\f(CW\*(C`qq()\*(C'\fR string passed to \f(CW\*(C`eval\*(C'\fR will not find string terminator.Sp\&\f(CW\*(C`qq()\*(C'\fR string passed to \f(CW\*(C`eval\*(C'\fR.Sp.Vb 5\& eval qq(\& foreach \e$y (keys %\e$x\e) {\& \e$count++;\& }\& );\&\& # perl4 runs this ok\& # perl5 prints: Can\*(Aqt find string terminator ")".Ve.Sh "\s-1DBM\s0 Traps".IX Subsection "DBM Traps"General \s-1DBM\s0 traps..IP "\(bu" 5Perl5 must have been linked with same dbm/ndbm as the default for \f(CW\*(C`dbmopen()\*(C'\fR.SpExisting dbm databases created under perl4 (or any other dbm/ndbm tool)may cause the same script, run under perl5, to fail. The build of perl5must have been linked with the same dbm/ndbm as the default for \f(CW\*(C`dbmopen()\*(C'\fRto function properly without \f(CW\*(C`tie\*(C'\fR'ing to an extension dbm implementation..Sp.Vb 2\& dbmopen (%dbm, "file", undef);\& print "ok\en";\&\& # perl4 prints: ok\& # perl5 prints: ok (IFF linked with \-ldbm or \-lndbm).Ve.IP "\(bu" 5\&\s-1DBM\s0 exceeding limit on the key/value size will cause perl5 to exit immediately.SpExisting dbm databases created under perl4 (or any other dbm/ndbm tool)may cause the same script, run under perl5, to fail. The error generatedwhen exceeding the limit on the key/value size will cause perl5 to exitimmediately..Sp.Vb 3\& dbmopen(DB, "testdb",0600) || die "couldn\*(Aqt open db! $!";\& $DB{\*(Aqtrap\*(Aq} = "x" x 1024; # value too large for most dbm/ndbm\& print "YUP\en";\&\& # perl4 prints:\& dbm store returned \-1, errno 28, key "trap" at \- line 3.\& YUP\&\& # perl5 prints:\& dbm store returned \-1, errno 28, key "trap" at \- line 3..Ve.Sh "Unclassified Traps".IX Subsection "Unclassified Traps"Everything else..IP "\(bu" 5\&\f(CW\*(C`require\*(C'\fR/\f(CW\*(C`do\*(C'\fR trap using returned value.SpIf the file doit.pl has:.Sp.Vb 5\& sub foo {\& $rc = do "./do.pl";\& return 8;\& }\& print &foo, "\en";.Ve.SpAnd the do.pl file has the following single line:.Sp.Vb 1\& return 3;.Ve.SpRunning doit.pl gives the following:.Sp.Vb 2\& # perl 4 prints: 3 (aborts the subroutine early)\& # perl 5 prints: 8.Ve.SpSame behavior if you replace \f(CW\*(C`do\*(C'\fR with \f(CW\*(C`require\*(C'\fR..IP "\(bu" 5\&\f(CW\*(C`split\*(C'\fR on empty string with \s-1LIMIT\s0 specified.Sp.Vb 2\& $string = \*(Aq\*(Aq;\& @list = split(/foo/, $string, 2).Ve.SpPerl4 returns a one element list containing the empty string but Perl5returns an empty list..PPAs always, if any of these are ever officially declared as bugs,they'll be fixed and removed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -