Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man1 / perltrap.1
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLTRAP 1"
132.TH PERLTRAP 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134perltrap \- Perl traps for the unwary
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137The biggest trap of all is forgetting to \f(CW\*(C`use warnings\*(C'\fR or use the \fB\-w\fR
138switch; see perllexwarn and perlrun. The second biggest trap is not
139making your entire program runnable under \f(CW\*(C`use strict\*(C'\fR. The third biggest
140trap is not reading the list of changes in this version of Perl; see
141perldelta.
142.Sh "Awk Traps"
143.IX Subsection "Awk Traps"
144Accustomed \fBawk\fR users should take special note of the following:
145.IP "\(bu" 4
146The English module, loaded via
147.Sp
148.Vb 1
149\& use English;
150.Ve
151.Sp
152allows you to refer to special variables (like \f(CW$/\fR) with names (like
153\&\f(CW$RS\fR), as though they were in \fBawk\fR; see perlvar for details.
154.IP "\(bu" 4
155Semicolons are required after all simple statements in Perl (except
156at the end of a block). Newline is not a statement delimiter.
157.IP "\(bu" 4
158Curly brackets are required on \f(CW\*(C`if\*(C'\fRs and \f(CW\*(C`while\*(C'\fRs.
159.IP "\(bu" 4
160Variables begin with \*(L"$\*(R", \*(L"@\*(R" or \*(L"%\*(R" in Perl.
161.IP "\(bu" 4
162Arrays index from 0. Likewise string positions in \fIsubstr()\fR and
163\&\fIindex()\fR.
164.IP "\(bu" 4
165You have to decide whether your array has numeric or string indices.
166.IP "\(bu" 4
167Hash values do not spring into existence upon mere reference.
168.IP "\(bu" 4
169You have to decide whether you want to use string or numeric
170comparisons.
171.IP "\(bu" 4
172Reading an input line does not split it for you. You get to split it
173to an array yourself. And the \fIsplit()\fR operator has different
174arguments than \fBawk\fR's.
175.IP "\(bu" 4
176The current input line is normally in \f(CW$_\fR, not \f(CW$0\fR. It generally does
177not have the newline stripped. ($0 is the name of the program
178executed.) See perlvar.
179.IP "\(bu" 4
180$<\fIdigit\fR> does not refer to fields\*(--it refers to substrings matched
181by the last match pattern.
182.IP "\(bu" 4
183The \fIprint()\fR statement does not add field and record separators unless
184you set \f(CW$,\fR and \f(CW\*(C`$\e\*(C'\fR. You can set \f(CW$OFS\fR and \f(CW$ORS\fR if you're using
185the English module.
186.IP "\(bu" 4
187You must open your files before you print to them.
188.IP "\(bu" 4
189The range operator is \*(L"..\*(R", not comma. The comma operator works as in
190C.
191.IP "\(bu" 4
192The match operator is \*(L"=~\*(R", not \*(L"~\*(R". (\*(L"~\*(R" is the one's complement
193operator, as in C.)
194.IP "\(bu" 4
195The exponentiation operator is \*(L"**\*(R", not \*(L"^\*(R". \*(L"^\*(R" is the \s-1XOR\s0
196operator, as in C. (You know, one could get the feeling that \fBawk\fR is
197basically incompatible with C.)
198.IP "\(bu" 4
199The concatenation operator is \*(L".\*(R", not the null string. (Using the
200null string would render \f(CW\*(C`/pat/ /pat/\*(C'\fR unparsable, because the third slash
201would be interpreted as a division operator\*(--the tokenizer is in fact
202slightly context sensitive for operators like \*(L"/\*(R", \*(L"?\*(R", and \*(L">\*(R".
203And in fact, \*(L".\*(R" itself can be the beginning of a number.)
204.IP "\(bu" 4
205The \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`exit\*(C'\fR, and \f(CW\*(C`continue\*(C'\fR keywords work differently.
206.IP "\(bu" 4
207The following variables work differently:
208.Sp
209.Vb 15
210\& Awk Perl
211\& ARGC scalar @ARGV (compare with $#ARGV)
212\& ARGV[0] $0
213\& FILENAME $ARGV
214\& FNR $. - something
215\& FS (whatever you like)
216\& NF $#Fld, or some such
217\& NR $.
218\& OFMT $#
219\& OFS $,
220\& ORS $\e
221\& RLENGTH length($&)
222\& RS $/
223\& RSTART length($`)
224\& SUBSEP $;
225.Ve
226.IP "\(bu" 4
227You cannot set \f(CW$RS\fR to a pattern, only a string.
228.IP "\(bu" 4
229When in doubt, run the \fBawk\fR construct through \fBa2p\fR and see what it
230gives you.
231.Sh "C Traps"
232.IX Subsection "C Traps"
233Cerebral C programmers should take note of the following:
234.IP "\(bu" 4
235Curly brackets are required on \f(CW\*(C`if\*(C'\fR's and \f(CW\*(C`while\*(C'\fR's.
236.IP "\(bu" 4
237You must use \f(CW\*(C`elsif\*(C'\fR rather than \f(CW\*(C`else if\*(C'\fR.
238.IP "\(bu" 4
239The \f(CW\*(C`break\*(C'\fR and \f(CW\*(C`continue\*(C'\fR keywords from C become in
240Perl \f(CW\*(C`last\*(C'\fR and \f(CW\*(C`next\*(C'\fR, respectively.
241Unlike in C, these do \fInot\fR work within a \f(CW\*(C`do { } while\*(C'\fR construct.
242.IP "\(bu" 4
243There's no switch statement. (But it's easy to build one on the fly.)
244.IP "\(bu" 4
245Variables begin with \*(L"$\*(R", \*(L"@\*(R" or \*(L"%\*(R" in Perl.
246.IP "\(bu" 4
247Comments begin with \*(L"#\*(R", not \*(L"/*\*(R".
248.IP "\(bu" 4
249You can't take the address of anything, although a similar operator
250in Perl is the backslash, which creates a reference.
251.IP "\(bu" 4
252\&\f(CW\*(C`ARGV\*(C'\fR must be capitalized. \f(CW$ARGV[0]\fR is C's \f(CW\*(C`argv[1]\*(C'\fR, and \f(CW\*(C`argv[0]\*(C'\fR
253ends up in \f(CW$0\fR.
254.IP "\(bu" 4
255System calls such as \fIlink()\fR, \fIunlink()\fR, \fIrename()\fR, etc. return nonzero for
256success, not 0. (\fIsystem()\fR, however, returns zero for success.)
257.IP "\(bu" 4
258Signal handlers deal with signal names, not numbers. Use \f(CW\*(C`kill \-l\*(C'\fR
259to find their names on your system.
260.Sh "Sed Traps"
261.IX Subsection "Sed Traps"
262Seasoned \fBsed\fR programmers should take note of the following:
263.IP "\(bu" 4
264Backreferences in substitutions use \*(L"$\*(R" rather than \*(L"\e\*(R".
265.IP "\(bu" 4
266The pattern matching metacharacters \*(L"(\*(R", \*(L")\*(R", and \*(L"|\*(R" do not have backslashes
267in front.
268.IP "\(bu" 4
269The range operator is \f(CW\*(C`...\*(C'\fR, rather than comma.
270.Sh "Shell Traps"
271.IX Subsection "Shell Traps"
272Sharp shell programmers should take note of the following:
273.IP "\(bu" 4
274The backtick operator does variable interpolation without regard to
275the presence of single quotes in the command.
276.IP "\(bu" 4
277The backtick operator does no translation of the return value, unlike \fBcsh\fR.
278.IP "\(bu" 4
279Shells (especially \fBcsh\fR) do several levels of substitution on each
280command line. Perl does substitution in only certain constructs
281such as double quotes, backticks, angle brackets, and search patterns.
282.IP "\(bu" 4
283Shells interpret scripts a little bit at a time. Perl compiles the
284entire program before executing it (except for \f(CW\*(C`BEGIN\*(C'\fR blocks, which
285execute at compile time).
286.IP "\(bu" 4
287The arguments are available via \f(CW@ARGV\fR, not \f(CW$1\fR, \f(CW$2\fR, etc.
288.IP "\(bu" 4
289The environment is not automatically made available as separate scalar
290variables.
291.Sh "Perl Traps"
292.IX Subsection "Perl Traps"
293Practicing Perl Programmers should take note of the following:
294.IP "\(bu" 4
295Remember that many operations behave differently in a list
296context than they do in a scalar one. See perldata for details.
297.IP "\(bu" 4
298Avoid barewords if you can, especially all lowercase ones.
299You can't tell by just looking at it whether a bareword is
300a function or a string. By using quotes on strings and
301parentheses on function calls, you won't ever get them confused.
302.IP "\(bu" 4
303You cannot discern from mere inspection which builtins
304are unary operators (like \fIchop()\fR and \fIchdir()\fR)
305and which are list operators (like \fIprint()\fR and \fIunlink()\fR).
306(Unless prototyped, user-defined subroutines can \fBonly\fR be list
307operators, never unary ones.) See perlop and perlsub.
308.IP "\(bu" 4
309People have a hard time remembering that some functions
310default to \f(CW$_\fR, or \f(CW@ARGV\fR, or whatever, but that others which
311you might expect to do not.
312.IP "\(bu" 4
313The <\s-1FH\s0> construct is not the name of the filehandle, it is a readline
314operation on that handle. The data read is assigned to \f(CW$_\fR only if the
315file read is the sole condition in a while loop:
316.Sp
317.Vb 3
318\& while (<FH>) { }
319\& while (defined($_ = <FH>)) { }..
320\& <FH>; # data discarded!
321.Ve
322.IP "\(bu" 4
323Remember not to use \f(CW\*(C`=\*(C'\fR when you need \f(CW\*(C`=~\*(C'\fR;
324these two constructs are quite different:
325.Sp
326.Vb 2
327\& $x = /foo/;
328\& $x =~ /foo/;
329.Ve
330.IP "\(bu" 4
331The \f(CW\*(C`do {}\*(C'\fR construct isn't a real loop that you can use
332loop control on.
333.IP "\(bu" 4
334Use \f(CW\*(C`my()\*(C'\fR for local variables whenever you can get away with
335it (but see perlform for where you can't).
336Using \f(CW\*(C`local()\*(C'\fR actually gives a local value to a global
337variable, which leaves you open to unforeseen side-effects
338of dynamic scoping.
339.IP "\(bu" 4
340If you localize an exported variable in a module, its exported value will
341not change. The local name becomes an alias to a new value but the
342external name is still an alias for the original.
343.Sh "Perl4 to Perl5 Traps"
344.IX Subsection "Perl4 to Perl5 Traps"
345Practicing Perl4 Programmers should take note of the following
346Perl4\-to\-Perl5 specific traps.
347.PP
348They're crudely ordered according to the following list:
349.IP "Discontinuance, Deprecation, and BugFix traps" 4
350.IX Item "Discontinuance, Deprecation, and BugFix traps"
351Anything that's been fixed as a perl4 bug, removed as a perl4 feature
352or deprecated as a perl4 feature with the intent to encourage usage of
353some other perl5 feature.
354.IP "Parsing Traps" 4
355.IX Item "Parsing Traps"
356Traps that appear to stem from the new parser.
357.IP "Numerical Traps" 4
358.IX Item "Numerical Traps"
359Traps having to do with numerical or mathematical operators.
360.IP "General data type traps" 4
361.IX Item "General data type traps"
362Traps involving perl standard data types.
363.IP "Context Traps \- scalar, list contexts" 4
364.IX Item "Context Traps - scalar, list contexts"
365Traps related to context within lists, scalar statements/declarations.
366.IP "Precedence Traps" 4
367.IX Item "Precedence Traps"
368Traps related to the precedence of parsing, evaluation, and execution of
369code.
370.IP "General Regular Expression Traps using s///, etc." 4
371.IX Item "General Regular Expression Traps using s///, etc."
372Traps related to the use of pattern matching.
373.IP "Subroutine, Signal, Sorting Traps" 4
374.IX Item "Subroutine, Signal, Sorting Traps"
375Traps related to the use of signals and signal handlers, general subroutines,
376and sorting, along with sorting subroutines.
377.IP "\s-1OS\s0 Traps" 4
378.IX Item "OS Traps"
379OS-specific traps.
380.IP "\s-1DBM\s0 Traps" 4
381.IX Item "DBM Traps"
382Traps specific to the use of \f(CW\*(C`dbmopen()\*(C'\fR, and specific dbm implementations.
383.IP "Unclassified Traps" 4
384.IX Item "Unclassified Traps"
385Everything else.
386.PP
387If you find an example of a conversion trap that is not listed here,
388please submit it to <\fIperlbug@perl.org\fR> for inclusion.
389Also note that at least some of these can be caught with the
390\&\f(CW\*(C`use warnings\*(C'\fR pragma or the \fB\-w\fR switch.
391.Sh "Discontinuance, Deprecation, and BugFix traps"
392.IX Subsection "Discontinuance, Deprecation, and BugFix traps"
393Anything that has been discontinued, deprecated, or fixed as
394a bug from perl4.
395.IP "\(bu Discontinuance" 4
396.IX Item "Discontinuance"
397Symbols starting with \*(L"_\*(R" are no longer forced into package main, except
398for \f(CW$_\fR itself (and \f(CW@_\fR, etc.).
399.Sp
400.Vb 2
401\& package test;
402\& $_legacy = 1;
403.Ve
404.Sp
405.Vb 2
406\& package main;
407\& print "\e$_legacy is ",$_legacy,"\en";
408.Ve
409.Sp
410.Vb 2
411\& # perl4 prints: $_legacy is 1
412\& # perl5 prints: $_legacy is
413.Ve
414.IP "\(bu Deprecation" 4
415.IX Item "Deprecation"
416Double-colon is now a valid package separator in a variable name. Thus these
417behave differently in perl4 vs. perl5, because the packages don't exist.
418.Sp
419.Vb 3
420\& $a=1;$b=2;$c=3;$var=4;
421\& print "$a::$b::$c ";
422\& print "$var::abc::xyz\en";
423.Ve
424.Sp
425.Vb 2
426\& # perl4 prints: 1::2::3 4::abc::xyz
427\& # perl5 prints: 3
428.Ve
429.Sp
430Given that \f(CW\*(C`::\*(C'\fR is now the preferred package delimiter, it is debatable
431whether this should be classed as a bug or not.
432(The older package delimiter, ' ,is used here)
433.Sp
434.Vb 2
435\& $x = 10 ;
436\& print "x=${'x}\en" ;
437.Ve
438.Sp
439.Vb 2
440\& # perl4 prints: x=10
441\& # perl5 prints: Can't find string terminator "'" anywhere before EOF
442.Ve
443.Sp
444You can avoid this problem, and remain compatible with perl4, if you
445always explicitly include the package name:
446.Sp
447.Vb 2
448\& $x = 10 ;
449\& print "x=${main'x}\en" ;
450.Ve
451.Sp
452Also see precedence traps, for parsing \f(CW$:\fR.
453.IP "\(bu BugFix" 4
454.IX Item "BugFix"
455The second and third arguments of \f(CW\*(C`splice()\*(C'\fR are now evaluated in scalar
456context (as the Camel says) rather than list context.
457.Sp
458.Vb 5
459\& sub sub1{return(0,2) } # return a 2-element list
460\& sub sub2{ return(1,2,3)} # return a 3-element list
461\& @a1 = ("a","b","c","d","e");
462\& @a2 = splice(@a1,&sub1,&sub2);
463\& print join(' ',@a2),"\en";
464.Ve
465.Sp
466.Vb 2
467\& # perl4 prints: a b
468\& # perl5 prints: c d e
469.Ve
470.IP "\(bu Discontinuance" 4
471.IX Item "Discontinuance"
472You can't do a \f(CW\*(C`goto\*(C'\fR into a block that is optimized away. Darn.
473.Sp
474.Vb 1
475\& goto marker1;
476.Ve
477.Sp
478.Vb 4
479\& for(1){
480\& marker1:
481\& print "Here I is!\en";
482\& }
483.Ve
484.Sp
485.Vb 2
486\& # perl4 prints: Here I is!
487\& # perl5 errors: Can't "goto" into the middle of a foreach loop
488.Ve
489.IP "\(bu Discontinuance" 4
490.IX Item "Discontinuance"
491It is no longer syntactically legal to use whitespace as the name
492of a variable, or as a delimiter for any kind of quote construct.
493Double darn.
494.Sp
495.Vb 3
496\& $a = ("foo bar");
497\& $b = q baz ;
498\& print "a is $a, b is $b\en";
499.Ve
500.Sp
501.Vb 2
502\& # perl4 prints: a is foo bar, b is baz
503\& # perl5 errors: Bareword found where operator expected
504.Ve
505.IP "\(bu Discontinuance" 4
506.IX Item "Discontinuance"
507The archaic while/if \s-1BLOCK\s0 \s-1BLOCK\s0 syntax is no longer supported.
508.Sp
509.Vb 6
510\& if { 1 } {
511\& print "True!";
512\& }
513\& else {
514\& print "False!";
515\& }
516.Ve
517.Sp
518.Vb 2
519\& # perl4 prints: True!
520\& # perl5 errors: syntax error at test.pl line 1, near "if {"
521.Ve
522.IP "\(bu BugFix" 4
523.IX Item "BugFix"
524The \f(CW\*(C`**\*(C'\fR operator now binds more tightly than unary minus.
525It was documented to work this way before, but didn't.
526.Sp
527.Vb 1
528\& print -4**2,"\en";
529.Ve
530.Sp
531.Vb 2
532\& # perl4 prints: 16
533\& # perl5 prints: -16
534.Ve
535.IP "\(bu Discontinuance" 4
536.IX Item "Discontinuance"
537The meaning of \f(CW\*(C`foreach{}\*(C'\fR has changed slightly when it is iterating over a
538list which is not an array. This used to assign the list to a
539temporary array, but no longer does so (for efficiency). This means
540that you'll now be iterating over the actual values, not over copies of
541the values. Modifications to the loop variable can change the original
542values.
543.Sp
544.Vb 5
545\& @list = ('ab','abc','bcd','def');
546\& foreach $var (grep(/ab/,@list)){
547\& $var = 1;
548\& }
549\& print (join(':',@list));
550.Ve
551.Sp
552.Vb 2
553\& # perl4 prints: ab:abc:bcd:def
554\& # perl5 prints: 1:1:bcd:def
555.Ve
556.Sp
557To retain Perl4 semantics you need to assign your list
558explicitly to a temporary array and then iterate over that. For
559example, you might need to change
560.Sp
561.Vb 1
562\& foreach $var (grep(/ab/,@list)){
563.Ve
564.Sp
565to
566.Sp
567.Vb 1
568\& foreach $var (@tmp = grep(/ab/,@list)){
569.Ve
570.Sp
571Otherwise changing \f(CW$var\fR will clobber the values of \f(CW@list\fR. (This most often
572happens when you use \f(CW$_\fR for the loop variable, and call subroutines in
573the loop that don't properly localize \f(CW$_\fR.)
574.IP "\(bu Discontinuance" 4
575.IX Item "Discontinuance"
576\&\f(CW\*(C`split\*(C'\fR with no arguments now behaves like \f(CW\*(C`split ' '\*(C'\fR (which doesn't
577return an initial null field if \f(CW$_\fR starts with whitespace), it used to
578behave like \f(CW\*(C`split /\es+/\*(C'\fR (which does).
579.Sp
580.Vb 2
581\& $_ = ' hi mom';
582\& print join(':', split);
583.Ve
584.Sp
585.Vb 2
586\& # perl4 prints: :hi:mom
587\& # perl5 prints: hi:mom
588.Ve
589.IP "\(bu BugFix" 4
590.IX Item "BugFix"
591Perl 4 would ignore any text which was attached to an \fB\-e\fR switch,
592always taking the code snippet from the following arg. Additionally, it
593would silently accept an \fB\-e\fR switch without a following arg. Both of
594these behaviors have been fixed.
595.Sp
596.Vb 1
597\& perl -e'print "attached to -e"' 'print "separate arg"'
598.Ve
599.Sp
600.Vb 2
601\& # perl4 prints: separate arg
602\& # perl5 prints: attached to -e
603.Ve
604.Sp
605.Vb 1
606\& perl -e
607.Ve
608.Sp
609.Vb 2
610\& # perl4 prints:
611\& # perl5 dies: No code specified for -e.
612.Ve
613.IP "\(bu Discontinuance" 4
614.IX Item "Discontinuance"
615In Perl 4 the return value of \f(CW\*(C`push\*(C'\fR was undocumented, but it was
616actually the last value being pushed onto the target list. In Perl 5
617the return value of \f(CW\*(C`push\*(C'\fR is documented, but has changed, it is the
618number of elements in the resulting list.
619.Sp
620.Vb 2
621\& @x = ('existing');
622\& print push(@x, 'first new', 'second new');
623.Ve
624.Sp
625.Vb 2
626\& # perl4 prints: second new
627\& # perl5 prints: 3
628.Ve
629.IP "\(bu Deprecation" 4
630.IX Item "Deprecation"
631Some error messages will be different.
632.IP "\(bu Discontinuance" 4
633.IX Item "Discontinuance"
634In Perl 4, if in list context the delimiters to the first argument of
635\&\f(CW\*(C`split()\*(C'\fR were \f(CW\*(C`??\*(C'\fR, the result would be placed in \f(CW@_\fR as well as
636being returned. Perl 5 has more respect for your subroutine arguments.
637.IP "\(bu Discontinuance" 4
638.IX Item "Discontinuance"
639Some bugs may have been inadvertently removed. :\-)
640.Sh "Parsing Traps"
641.IX Subsection "Parsing Traps"
642Perl4\-to\-Perl5 traps from having to do with parsing.
643.IP "\(bu Parsing" 4
644.IX Item "Parsing"
645Note the space between . and =
646.Sp
647.Vb 2
648\& $string . = "more string";
649\& print $string;
650.Ve
651.Sp
652.Vb 2
653\& # perl4 prints: more string
654\& # perl5 prints: syntax error at - line 1, near ". ="
655.Ve
656.IP "\(bu Parsing" 4
657.IX Item "Parsing"
658Better parsing in perl 5
659.Sp
660.Vb 3
661\& sub foo {}
662\& &foo
663\& print("hello, world\en");
664.Ve
665.Sp
666.Vb 2
667\& # perl4 prints: hello, world
668\& # perl5 prints: syntax error
669.Ve
670.IP "\(bu Parsing" 4
671.IX Item "Parsing"
672\&\*(L"if it looks like a function, it is a function\*(R" rule.
673.Sp
674.Vb 2
675\& print
676\& ($foo == 1) ? "is one\en" : "is zero\en";
677.Ve
678.Sp
679.Vb 2
680\& # perl4 prints: is zero
681\& # perl5 warns: "Useless use of a constant in void context" if using -w
682.Ve
683.IP "\(bu Parsing" 4
684.IX Item "Parsing"
685String interpolation of the \f(CW$#array\fR construct differs when braces
686are to used around the name.
687.Sp
688.Vb 2
689\& @a = (1..3);
690\& print "${#a}";
691.Ve
692.Sp
693.Vb 2
694\& # perl4 prints: 2
695\& # perl5 fails with syntax error
696.Ve
697.Sp
698.Vb 2
699\& @ = (1..3);
700\& print "$#{a}";
701.Ve
702.Sp
703.Vb 2
704\& # perl4 prints: {a}
705\& # perl5 prints: 2
706.Ve
707.IP "\(bu Parsing" 4
708.IX Item "Parsing"
709When perl sees \f(CW\*(C`map {\*(C'\fR (or \f(CW\*(C`grep {\*(C'\fR), it has to guess whether the \f(CW\*(C`{\*(C'\fR
710starts a \s-1BLOCK\s0 or a hash reference. If it guesses wrong, it will report
711a syntax error near the \f(CW\*(C`}\*(C'\fR and the missing (or unexpected) comma.
712.Sp
713Use unary \f(CW\*(C`+\*(C'\fR before \f(CW\*(C`{\*(C'\fR on a hash reference, and unary \f(CW\*(C`+\*(C'\fR applied
714to the first thing in a \s-1BLOCK\s0 (after \f(CW\*(C`{\*(C'\fR), for perl to guess right all
715the time. (See \*(L"map\*(R" in perlfunc.)
716.Sh "Numerical Traps"
717.IX Subsection "Numerical Traps"
718Perl4\-to\-Perl5 traps having to do with numerical operators,
719operands, or output from same.
720.IP "\(bu Numerical" 5
721.IX Item "Numerical"
722Formatted output and significant digits. In general, Perl 5
723tries to be more precise. For example, on a Solaris Sparc:
724.Sp
725.Vb 2
726\& print 7.373504 - 0, "\en";
727\& printf "%20.18f\en", 7.373504 - 0;
728.Ve
729.Sp
730.Vb 3
731\& # Perl4 prints:
732\& 7.3750399999999996141
733\& 7.375039999999999614
734.Ve
735.Sp
736.Vb 3
737\& # Perl5 prints:
738\& 7.373504
739\& 7.375039999999999614
740.Ve
741.Sp
742Notice how the first result looks better in Perl 5.
743.Sp
744Your results may vary, since your floating point formatting routines
745and even floating point format may be slightly different.
746.IP "\(bu Numerical" 5
747.IX Item "Numerical"
748This specific item has been deleted. It demonstrated how the auto-increment
749operator would not catch when a number went over the signed int limit. Fixed
750in version 5.003_04. But always be wary when using large integers.
751If in doubt:
752.Sp
753.Vb 1
754\& use Math::BigInt;
755.Ve
756.IP "\(bu Numerical" 5
757.IX Item "Numerical"
758Assignment of return values from numeric equality tests
759does not work in perl5 when the test evaluates to false (0).
760Logical tests now return a null, instead of 0
761.Sp
762.Vb 2
763\& $p = ($test == 1);
764\& print $p,"\en";
765.Ve
766.Sp
767.Vb 2
768\& # perl4 prints: 0
769\& # perl5 prints:
770.Ve
771.Sp
772Also see \*(L"General Regular Expression Traps using s///, etc.\*(R"
773for another example of this new feature...
774.IP "\(bu Bitwise string ops" 5
775.IX Item "Bitwise string ops"
776When bitwise operators which can operate upon either numbers or
777strings (\f(CW\*(C`& | ^ ~\*(C'\fR) are given only strings as arguments, perl4 would
778treat the operands as bitstrings so long as the program contained a call
779to the \f(CW\*(C`vec()\*(C'\fR function. perl5 treats the string operands as bitstrings.
780(See \*(L"Bitwise String Operators\*(R" in perlop for more details.)
781.Sp
782.Vb 6
783\& $fred = "10";
784\& $barney = "12";
785\& $betty = $fred & $barney;
786\& print "$betty\en";
787\& # Uncomment the next line to change perl4's behavior
788\& # ($dummy) = vec("dummy", 0, 0);
789.Ve
790.Sp
791.Vb 2
792\& # Perl4 prints:
793\& 8
794.Ve
795.Sp
796.Vb 2
797\& # Perl5 prints:
798\& 10
799.Ve
800.Sp
801.Vb 2
802\& # If vec() is used anywhere in the program, both print:
803\& 10
804.Ve
805.Sh "General data type traps"
806.IX Subsection "General data type traps"
807Perl4\-to\-Perl5 traps involving most data\-types, and their usage
808within certain expressions and/or context.
809.IP "\(bu (Arrays)" 5
810.IX Item "(Arrays)"
811Negative array subscripts now count from the end of the array.
812.Sp
813.Vb 2
814\& @a = (1, 2, 3, 4, 5);
815\& print "The third element of the array is $a[3] also expressed as $a[-2] \en";
816.Ve
817.Sp
818.Vb 2
819\& # perl4 prints: The third element of the array is 4 also expressed as
820\& # perl5 prints: The third element of the array is 4 also expressed as 4
821.Ve
822.IP "\(bu (Arrays)" 5
823.IX Item "(Arrays)"
824Setting \f(CW$#array\fR lower now discards array elements, and makes them
825impossible to recover.
826.Sp
827.Vb 6
828\& @a = (a,b,c,d,e);
829\& print "Before: ",join('',@a);
830\& $#a =1;
831\& print ", After: ",join('',@a);
832\& $#a =3;
833\& print ", Recovered: ",join('',@a),"\en";
834.Ve
835.Sp
836.Vb 2
837\& # perl4 prints: Before: abcde, After: ab, Recovered: abcd
838\& # perl5 prints: Before: abcde, After: ab, Recovered: ab
839.Ve
840.IP "\(bu (Hashes)" 5
841.IX Item "(Hashes)"
842Hashes get defined before use
843.Sp
844.Vb 4
845\& local($s,@a,%h);
846\& die "scalar \e$s defined" if defined($s);
847\& die "array \e@a defined" if defined(@a);
848\& die "hash \e%h defined" if defined(%h);
849.Ve
850.Sp
851.Vb 2
852\& # perl4 prints:
853\& # perl5 dies: hash %h defined
854.Ve
855.Sp
856Perl will now generate a warning when it sees defined(@a) and
857defined(%h).
858.IP "\(bu (Globs)" 5
859.IX Item "(Globs)"
860glob assignment from variable to variable will fail if the assigned
861variable is localized subsequent to the assignment
862.Sp
863.Vb 4
864\& @a = ("This is Perl 4");
865\& *b = *a;
866\& local(@a);
867\& print @b,"\en";
868.Ve
869.Sp
870.Vb 2
871\& # perl4 prints: This is Perl 4
872\& # perl5 prints:
873.Ve
874.IP "\(bu (Globs)" 5
875.IX Item "(Globs)"
876Assigning \f(CW\*(C`undef\*(C'\fR to a glob has no effect in Perl 5. In Perl 4
877it undefines the associated scalar (but may have other side effects
878including SEGVs). Perl 5 will also warn if \f(CW\*(C`undef\*(C'\fR is assigned to a
879typeglob. (Note that assigning \f(CW\*(C`undef\*(C'\fR to a typeglob is different
880than calling the \f(CW\*(C`undef\*(C'\fR function on a typeglob (\f(CW\*(C`undef *foo\*(C'\fR), which
881has quite a few effects.
882.Sp
883.Vb 3
884\& $foo = "bar";
885\& *foo = undef;
886\& print $foo;
887.Ve
888.Sp
889.Vb 4
890\& # perl4 prints:
891\& # perl4 warns: "Use of uninitialized variable" if using -w
892\& # perl5 prints: bar
893\& # perl5 warns: "Undefined value assigned to typeglob" if using -w
894.Ve
895.IP "\(bu (Scalar String)" 5
896.IX Item "(Scalar String)"
897Changes in unary negation (of strings)
898This change effects both the return value and what it
899does to auto(magic)increment.
900.Sp
901.Vb 4
902\& $x = "aaa";
903\& print ++$x," : ";
904\& print -$x," : ";
905\& print ++$x,"\en";
906.Ve
907.Sp
908.Vb 2
909\& # perl4 prints: aab : -0 : 1
910\& # perl5 prints: aab : -aab : aac
911.Ve
912.IP "\(bu (Constants)" 5
913.IX Item "(Constants)"
914perl 4 lets you modify constants:
915.Sp
916.Vb 10
917\& $foo = "x";
918\& &mod($foo);
919\& for ($x = 0; $x < 3; $x++) {
920\& &mod("a");
921\& }
922\& sub mod {
923\& print "before: $_[0]";
924\& $_[0] = "m";
925\& print " after: $_[0]\en";
926\& }
927.Ve
928.Sp
929.Vb 5
930\& # perl4:
931\& # before: x after: m
932\& # before: a after: m
933\& # before: m after: m
934\& # before: m after: m
935.Ve
936.Sp
937.Vb 4
938\& # Perl5:
939\& # before: x after: m
940\& # Modification of a read-only value attempted at foo.pl line 12.
941\& # before: a
942.Ve
943.IP "\(bu (Scalars)" 5
944.IX Item "(Scalars)"
945The behavior is slightly different for:
946.Sp
947.Vb 1
948\& print "$x", defined $x
949.Ve
950.Sp
951.Vb 2
952\& # perl 4: 1
953\& # perl 5: <no output, $x is not called into existence>
954.Ve
955.IP "\(bu (Variable Suicide)" 5
956.IX Item "(Variable Suicide)"
957Variable suicide behavior is more consistent under Perl 5.
958Perl5 exhibits the same behavior for hashes and scalars,
959that perl4 exhibits for only scalars.
960.Sp
961.Vb 4
962\& $aGlobal{ "aKey" } = "global value";
963\& print "MAIN:", $aGlobal{"aKey"}, "\en";
964\& $GlobalLevel = 0;
965\& &test( *aGlobal );
966.Ve
967.Sp
968.Vb 11
969\& sub test {
970\& local( *theArgument ) = @_;
971\& local( %aNewLocal ); # perl 4 != 5.001l,m
972\& $aNewLocal{"aKey"} = "this should never appear";
973\& print "SUB: ", $theArgument{"aKey"}, "\en";
974\& $aNewLocal{"aKey"} = "level $GlobalLevel"; # what should print
975\& $GlobalLevel++;
976\& if( $GlobalLevel<4 ) {
977\& &test( *aNewLocal );
978\& }
979\& }
980.Ve
981.Sp
982.Vb 6
983\& # Perl4:
984\& # MAIN:global value
985\& # SUB: global value
986\& # SUB: level 0
987\& # SUB: level 1
988\& # SUB: level 2
989.Ve
990.Sp
991.Vb 6
992\& # Perl5:
993\& # MAIN:global value
994\& # SUB: global value
995\& # SUB: this should never appear
996\& # SUB: this should never appear
997\& # SUB: this should never appear
998.Ve
999.Sh "Context Traps \- scalar, list contexts"
1000.IX Subsection "Context Traps - scalar, list contexts"
1001.IP "\(bu (list context)" 5
1002.IX Item "(list context)"
1003The elements of argument lists for formats are now evaluated in list
1004context. This means you can interpolate list values now.
1005.Sp
1006.Vb 6
1007\& @fmt = ("foo","bar","baz");
1008\& format STDOUT=
1009\& @<<<<< @||||| @>>>>>
1010\& @fmt;
1011\& .
1012\& write;
1013.Ve
1014.Sp
1015.Vb 2
1016\& # perl4 errors: Please use commas to separate fields in file
1017\& # perl5 prints: foo bar baz
1018.Ve
1019.IP "\(bu (scalar context)" 5
1020.IX Item "(scalar context)"
1021The \f(CW\*(C`caller()\*(C'\fR function now returns a false value in a scalar context
1022if there is no caller. This lets library files determine if they're
1023being required.
1024.Sp
1025.Vb 1
1026\& caller() ? (print "You rang?\en") : (print "Got a 0\en");
1027.Ve
1028.Sp
1029.Vb 2
1030\& # perl4 errors: There is no caller
1031\& # perl5 prints: Got a 0
1032.Ve
1033.IP "\(bu (scalar context)" 5
1034.IX Item "(scalar context)"
1035The comma operator in a scalar context is now guaranteed to give a
1036scalar context to its arguments.
1037.Sp
1038.Vb 3
1039\& @y= ('a','b','c');
1040\& $x = (1, 2, @y);
1041\& print "x = $x\en";
1042.Ve
1043.Sp
1044.Vb 2
1045\& # Perl4 prints: x = c # Thinks list context interpolates list
1046\& # Perl5 prints: x = 3 # Knows scalar uses length of list
1047.Ve
1048.IP "\(bu (list, builtin)" 5
1049.IX Item "(list, builtin)"
1050\&\f(CW\*(C`sprintf()\*(C'\fR is prototyped as ($;@), so its first argument is given scalar
1051context. Thus, if passed an array, it will probably not do what you want,
1052unlike Perl 4:
1053.Sp
1054.Vb 3
1055\& @z = ('%s%s', 'foo', 'bar');
1056\& $x = sprintf(@z);
1057\& print $x;
1058.Ve
1059.Sp
1060.Vb 2
1061\& # perl4 prints: foobar
1062\& # perl5 prints: 3
1063.Ve
1064.Sp
1065\&\f(CW\*(C`printf()\*(C'\fR works the same as it did in Perl 4, though:
1066.Sp
1067.Vb 2
1068\& @z = ('%s%s', 'foo', 'bar');
1069\& printf STDOUT (@z);
1070.Ve
1071.Sp
1072.Vb 2
1073\& # perl4 prints: foobar
1074\& # perl5 prints: foobar
1075.Ve
1076.Sh "Precedence Traps"
1077.IX Subsection "Precedence Traps"
1078Perl4\-to\-Perl5 traps involving precedence order.
1079.PP
1080Perl 4 has almost the same precedence rules as Perl 5 for the operators
1081that they both have. Perl 4 however, seems to have had some
1082inconsistencies that made the behavior differ from what was documented.
1083.IP "\(bu Precedence" 5
1084.IX Item "Precedence"
1085\&\s-1LHS\s0 vs. \s-1RHS\s0 of any assignment operator. \s-1LHS\s0 is evaluated first
1086in perl4, second in perl5; this can affect the relationship
1087between side-effects in sub\-expressions.
1088.Sp
1089.Vb 3
1090\& @arr = ( 'left', 'right' );
1091\& $a{shift @arr} = shift @arr;
1092\& print join( ' ', keys %a );
1093.Ve
1094.Sp
1095.Vb 2
1096\& # perl4 prints: left
1097\& # perl5 prints: right
1098.Ve
1099.IP "\(bu Precedence" 5
1100.IX Item "Precedence"
1101These are now semantic errors because of precedence:
1102.Sp
1103.Vb 6
1104\& @list = (1,2,3,4,5);
1105\& %map = ("a",1,"b",2,"c",3,"d",4);
1106\& $n = shift @list + 2; # first item in list plus 2
1107\& print "n is $n, ";
1108\& $m = keys %map + 2; # number of items in hash plus 2
1109\& print "m is $m\en";
1110.Ve
1111.Sp
1112.Vb 2
1113\& # perl4 prints: n is 3, m is 6
1114\& # perl5 errors and fails to compile
1115.Ve
1116.IP "\(bu Precedence" 5
1117.IX Item "Precedence"
1118The precedence of assignment operators is now the same as the precedence
1119of assignment. Perl 4 mistakenly gave them the precedence of the associated
1120operator. So you now must parenthesize them in expressions like
1121.Sp
1122.Vb 1
1123\& /foo/ ? ($a += 2) : ($a -= 2);
1124.Ve
1125.Sp
1126Otherwise
1127.Sp
1128.Vb 1
1129\& /foo/ ? $a += 2 : $a -= 2
1130.Ve
1131.Sp
1132would be erroneously parsed as
1133.Sp
1134.Vb 1
1135\& (/foo/ ? $a += 2 : $a) -= 2;
1136.Ve
1137.Sp
1138On the other hand,
1139.Sp
1140.Vb 1
1141\& $a += /foo/ ? 1 : 2;
1142.Ve
1143.Sp
1144now works as a C programmer would expect.
1145.IP "\(bu Precedence" 5
1146.IX Item "Precedence"
1147.Vb 1
1148\& open FOO || die;
1149.Ve
1150.Sp
1151is now incorrect. You need parentheses around the filehandle.
1152Otherwise, perl5 leaves the statement as its default precedence:
1153.Sp
1154.Vb 1
1155\& open(FOO || die);
1156.Ve
1157.Sp
1158.Vb 2
1159\& # perl4 opens or dies
1160\& # perl5 opens FOO, dying only if 'FOO' is false, i.e. never
1161.Ve
1162.IP "\(bu Precedence" 5
1163.IX Item "Precedence"
1164perl4 gives the special variable, \f(CW$:\fR precedence, where perl5
1165treats \f(CW$::\fR as main \f(CW\*(C`package\*(C'\fR
1166.Sp
1167.Vb 1
1168\& $a = "x"; print "$::a";
1169.Ve
1170.Sp
1171.Vb 2
1172\& # perl 4 prints: -:a
1173\& # perl 5 prints: x
1174.Ve
1175.IP "\(bu Precedence" 5
1176.IX Item "Precedence"
1177perl4 had buggy precedence for the file test operators vis-a-vis
1178the assignment operators. Thus, although the precedence table
1179for perl4 leads one to believe \f(CW\*(C`\-e $foo .= "q"\*(C'\fR should parse as
1180\&\f(CW\*(C`((\-e $foo) .= "q")\*(C'\fR, it actually parses as \f(CW\*(C`(\-e ($foo .= "q"))\*(C'\fR.
1181In perl5, the precedence is as documented.
1182.Sp
1183.Vb 1
1184\& -e $foo .= "q"
1185.Ve
1186.Sp
1187.Vb 2
1188\& # perl4 prints: no output
1189\& # perl5 prints: Can't modify -e in concatenation
1190.Ve
1191.IP "\(bu Precedence" 5
1192.IX Item "Precedence"
1193In perl4, \fIkeys()\fR, \fIeach()\fR and \fIvalues()\fR were special high-precedence operators
1194that operated on a single hash, but in perl5, they are regular named unary
1195operators. As documented, named unary operators have lower precedence
1196than the arithmetic and concatenation operators \f(CW\*(C`+ \- .\*(C'\fR, but the perl4
1197variants of these operators actually bind tighter than \f(CW\*(C`+ \- .\*(C'\fR.
1198Thus, for:
1199.Sp
1200.Vb 2
1201\& %foo = 1..10;
1202\& print keys %foo - 1
1203.Ve
1204.Sp
1205.Vb 2
1206\& # perl4 prints: 4
1207\& # perl5 prints: Type of arg 1 to keys must be hash (not subtraction)
1208.Ve
1209.Sp
1210The perl4 behavior was probably more useful, if less consistent.
1211.Sh "General Regular Expression Traps using s///, etc."
1212.IX Subsection "General Regular Expression Traps using s///, etc."
1213All types of \s-1RE\s0 traps.
1214.IP "\(bu Regular Expression" 5
1215.IX Item "Regular Expression"
1216\&\f(CW\*(C`s'$lhs'$rhs'\*(C'\fR now does no interpolation on either side. It used to
1217interpolate \f(CW$lhs\fR but not \f(CW$rhs\fR. (And still does not match a literal
1218\&'$' in string)
1219.Sp
1220.Vb 4
1221\& $a=1;$b=2;
1222\& $string = '1 2 $a $b';
1223\& $string =~ s'$a'$b';
1224\& print $string,"\en";
1225.Ve
1226.Sp
1227.Vb 2
1228\& # perl4 prints: $b 2 $a $b
1229\& # perl5 prints: 1 2 $a $b
1230.Ve
1231.IP "\(bu Regular Expression" 5
1232.IX Item "Regular Expression"
1233\&\f(CW\*(C`m//g\*(C'\fR now attaches its state to the searched string rather than the
1234regular expression. (Once the scope of a block is left for the sub, the
1235state of the searched string is lost)
1236.Sp
1237.Vb 5
1238\& $_ = "ababab";
1239\& while(m/ab/g){
1240\& &doit("blah");
1241\& }
1242\& sub doit{local($_) = shift; print "Got $_ "}
1243.Ve
1244.Sp
1245.Vb 2
1246\& # perl4 prints: Got blah Got blah Got blah Got blah
1247\& # perl5 prints: infinite loop blah...
1248.Ve
1249.IP "\(bu Regular Expression" 5
1250.IX Item "Regular Expression"
1251Currently, if you use the \f(CW\*(C`m//o\*(C'\fR qualifier on a regular expression
1252within an anonymous sub, \fIall\fR closures generated from that anonymous
1253sub will use the regular expression as it was compiled when it was used
1254the very first time in any such closure. For instance, if you say
1255.Sp
1256.Vb 9
1257\& sub build_match {
1258\& my($left,$right) = @_;
1259\& return sub { $_[0] =~ /$left stuff $right/o; };
1260\& }
1261\& $good = build_match('foo','bar');
1262\& $bad = build_match('baz','blarch');
1263\& print $good->('foo stuff bar') ? "ok\en" : "not ok\en";
1264\& print $bad->('baz stuff blarch') ? "ok\en" : "not ok\en";
1265\& print $bad->('foo stuff bar') ? "not ok\en" : "ok\en";
1266.Ve
1267.Sp
1268For most builds of Perl5, this will print:
1269ok
1270not ok
1271not ok
1272.Sp
1273\&\fIbuild_match()\fR will always return a sub which matches the contents of
1274\&\f(CW$left\fR and \f(CW$right\fR as they were the \fIfirst\fR time that \fIbuild_match()\fR
1275was called, not as they are in the current call.
1276.IP "\(bu Regular Expression" 5
1277.IX Item "Regular Expression"
1278If no parentheses are used in a match, Perl4 sets \f(CW$+\fR to
1279the whole match, just like \f(CW$&\fR. Perl5 does not.
1280.Sp
1281.Vb 2
1282\& "abcdef" =~ /b.*e/;
1283\& print "\e$+ = $+\en";
1284.Ve
1285.Sp
1286.Vb 2
1287\& # perl4 prints: bcde
1288\& # perl5 prints:
1289.Ve
1290.IP "\(bu Regular Expression" 5
1291.IX Item "Regular Expression"
1292substitution now returns the null string if it fails
1293.Sp
1294.Vb 3
1295\& $string = "test";
1296\& $value = ($string =~ s/foo//);
1297\& print $value, "\en";
1298.Ve
1299.Sp
1300.Vb 2
1301\& # perl4 prints: 0
1302\& # perl5 prints:
1303.Ve
1304.Sp
1305Also see \*(L"Numerical Traps\*(R" for another example of this new feature.
1306.IP "\(bu Regular Expression" 5
1307.IX Item "Regular Expression"
1308\&\f(CW\*(C`s`lhs`rhs`\*(C'\fR (using backticks) is now a normal substitution, with no
1309backtick expansion
1310.Sp
1311.Vb 3
1312\& $string = "";
1313\& $string =~ s`^`hostname`;
1314\& print $string, "\en";
1315.Ve
1316.Sp
1317.Vb 2
1318\& # perl4 prints: <the local hostname>
1319\& # perl5 prints: hostname
1320.Ve
1321.IP "\(bu Regular Expression" 5
1322.IX Item "Regular Expression"
1323Stricter parsing of variables used in regular expressions
1324.Sp
1325.Vb 1
1326\& s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;
1327.Ve
1328.Sp
1329.Vb 2
1330\& # perl4: compiles w/o error
1331\& # perl5: with Scalar found where operator expected ..., near "$opt$plus"
1332.Ve
1333.Sp
1334an added component of this example, apparently from the same script, is
1335the actual value of the s'd string after the substitution.
1336\&\f(CW\*(C`[$opt]\*(C'\fR is a character class in perl4 and an array subscript in perl5
1337.Sp
1338.Vb 5
1339\& $grpc = 'a';
1340\& $opt = 'r';
1341\& $_ = 'bar';
1342\& s/^([^$grpc]*$grpc[$opt]?)/foo/;
1343\& print ;
1344.Ve
1345.Sp
1346.Vb 2
1347\& # perl4 prints: foo
1348\& # perl5 prints: foobar
1349.Ve
1350.IP "\(bu Regular Expression" 5
1351.IX Item "Regular Expression"
1352Under perl5, \f(CW\*(C`m?x?\*(C'\fR matches only once, like \f(CW\*(C`?x?\*(C'\fR. Under perl4, it matched
1353repeatedly, like \f(CW\*(C`/x/\*(C'\fR or \f(CW\*(C`m!x!\*(C'\fR.
1354.Sp
1355.Vb 10
1356\& $test = "once";
1357\& sub match { $test =~ m?once?; }
1358\& &match();
1359\& if( &match() ) {
1360\& # m?x? matches more then once
1361\& print "perl4\en";
1362\& } else {
1363\& # m?x? matches only once
1364\& print "perl5\en";
1365\& }
1366.Ve
1367.Sp
1368.Vb 2
1369\& # perl4 prints: perl4
1370\& # perl5 prints: perl5
1371.Ve
1372.Sh "Subroutine, Signal, Sorting Traps"
1373.IX Subsection "Subroutine, Signal, Sorting Traps"
1374The general group of Perl4\-to\-Perl5 traps having to do with
1375Signals, Sorting, and their related subroutines, as well as
1376general subroutine traps. Includes some OS-Specific traps.
1377.IP "\(bu (Signals)" 5
1378.IX Item "(Signals)"
1379Barewords that used to look like strings to Perl will now look like subroutine
1380calls if a subroutine by that name is defined before the compiler sees them.
1381.Sp
1382.Vb 3
1383\& sub SeeYa { warn"Hasta la vista, baby!" }
1384\& $SIG{'TERM'} = SeeYa;
1385\& print "SIGTERM is now $SIG{'TERM'}\en";
1386.Ve
1387.Sp
1388.Vb 2
1389\& # perl4 prints: SIGTERM is now main'SeeYa
1390\& # perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!")
1391.Ve
1392.Sp
1393Use \fB\-w\fR to catch this one
1394.IP "\(bu (Sort Subroutine)" 5
1395.IX Item "(Sort Subroutine)"
1396reverse is no longer allowed as the name of a sort subroutine.
1397.Sp
1398.Vb 2
1399\& sub reverse{ print "yup "; $a <=> $b }
1400\& print sort reverse (2,1,3);
1401.Ve
1402.Sp
1403.Vb 3
1404\& # perl4 prints: yup yup 123
1405\& # perl5 prints: 123
1406\& # perl5 warns (if using -w): Ambiguous call resolved as CORE::reverse()
1407.Ve
1408.IP "\(bu \fIwarn()\fR won't let you specify a filehandle." 5
1409.IX Item "warn() won't let you specify a filehandle."
1410Although it _always_ printed to \s-1STDERR\s0, \fIwarn()\fR would let you specify a
1411filehandle in perl4. With perl5 it does not.
1412.Sp
1413.Vb 1
1414\& warn STDERR "Foo!";
1415.Ve
1416.Sp
1417.Vb 2
1418\& # perl4 prints: Foo!
1419\& # perl5 prints: String found where operator expected
1420.Ve
1421.Sh "\s-1OS\s0 Traps"
1422.IX Subsection "OS Traps"
1423.IP "\(bu (SysV)" 5
1424.IX Item "(SysV)"
1425Under \s-1HPUX\s0, and some other SysV OSes, one had to reset any signal handler,
1426within the signal handler function, each time a signal was handled with
1427perl4. With perl5, the reset is now done correctly. Any code relying
1428on the handler _not_ being reset will have to be reworked.
1429.Sp
1430Since version 5.002, Perl uses \fIsigaction()\fR under SysV.
1431.Sp
1432.Vb 4
1433\& sub gotit {
1434\& print "Got @_... ";
1435\& }
1436\& $SIG{'INT'} = 'gotit';
1437.Ve
1438.Sp
1439.Vb 9
1440\& $| = 1;
1441\& $pid = fork;
1442\& if ($pid) {
1443\& kill('INT', $pid);
1444\& sleep(1);
1445\& kill('INT', $pid);
1446\& } else {
1447\& while (1) {sleep(10);}
1448\& }
1449.Ve
1450.Sp
1451.Vb 2
1452\& # perl4 (HPUX) prints: Got INT...
1453\& # perl5 (HPUX) prints: Got INT... Got INT...
1454.Ve
1455.IP "\(bu (SysV)" 5
1456.IX Item "(SysV)"
1457Under SysV OSes, \f(CW\*(C`seek()\*(C'\fR on a file opened to append \f(CW\*(C`>>\*(C'\fR now does
1458the right thing w.r.t. the \fIfopen()\fR manpage. e.g., \- When a file is opened
1459for append, it is impossible to overwrite information already in
1460the file.
1461.Sp
1462.Vb 8
1463\& open(TEST,">>seek.test");
1464\& $start = tell TEST ;
1465\& foreach(1 .. 9){
1466\& print TEST "$_ ";
1467\& }
1468\& $end = tell TEST ;
1469\& seek(TEST,$start,0);
1470\& print TEST "18 characters here";
1471.Ve
1472.Sp
1473.Vb 2
1474\& # perl4 (solaris) seek.test has: 18 characters here
1475\& # perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here
1476.Ve
1477.Sh "Interpolation Traps"
1478.IX Subsection "Interpolation Traps"
1479Perl4\-to\-Perl5 traps having to do with how things get interpolated
1480within certain expressions, statements, contexts, or whatever.
1481.IP "\(bu Interpolation" 5
1482.IX Item "Interpolation"
1483@ now always interpolates an array in double-quotish strings.
1484.Sp
1485.Vb 1
1486\& print "To: someone@somewhere.com\en";
1487.Ve
1488.Sp
1489.Vb 3
1490\& # perl4 prints: To:someone@somewhere.com
1491\& # perl < 5.6.1, error : In string, @somewhere now must be written as \e@somewhere
1492\& # perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string
1493.Ve
1494.IP "\(bu Interpolation" 5
1495.IX Item "Interpolation"
1496Double-quoted strings may no longer end with an unescaped $ or @.
1497.Sp
1498.Vb 3
1499\& $foo = "foo$";
1500\& $bar = "bar@";
1501\& print "foo is $foo, bar is $bar\en";
1502.Ve
1503.Sp
1504.Vb 2
1505\& # perl4 prints: foo is foo$, bar is bar@
1506\& # perl5 errors: Final $ should be \e$ or $name
1507.Ve
1508.Sp
1509Note: perl5 \s-1DOES\s0 \s-1NOT\s0 error on the terminating @ in \f(CW$bar\fR
1510.IP "\(bu Interpolation" 5
1511.IX Item "Interpolation"
1512Perl now sometimes evaluates arbitrary expressions inside braces that occur
1513within double quotes (usually when the opening brace is preceded by \f(CW\*(C`$\*(C'\fR
1514or \f(CW\*(C`@\*(C'\fR).
1515.Sp
1516.Vb 5
1517\& @www = "buz";
1518\& $foo = "foo";
1519\& $bar = "bar";
1520\& sub foo { return "bar" };
1521\& print "|@{w.w.w}|${main'foo}|";
1522.Ve
1523.Sp
1524.Vb 2
1525\& # perl4 prints: |@{w.w.w}|foo|
1526\& # perl5 prints: |buz|bar|
1527.Ve
1528.Sp
1529Note that you can \f(CW\*(C`use strict;\*(C'\fR to ward off such trappiness under perl5.
1530.IP "\(bu Interpolation" 5
1531.IX Item "Interpolation"
1532The construct \*(L"this is $$x\*(R" used to interpolate the pid at that point, but
1533now tries to dereference \f(CW$x\fR. \f(CW$$\fR by itself still works fine, however.
1534.Sp
1535.Vb 3
1536\& $s = "a reference";
1537\& $x = *s;
1538\& print "this is $$x\en";
1539.Ve
1540.Sp
1541.Vb 2
1542\& # perl4 prints: this is XXXx (XXX is the current pid)
1543\& # perl5 prints: this is a reference
1544.Ve
1545.IP "\(bu Interpolation" 5
1546.IX Item "Interpolation"
1547Creation of hashes on the fly with \f(CW\*(C`eval "EXPR"\*(C'\fR now requires either both
1548\&\f(CW\*(C`$\*(C'\fR's to be protected in the specification of the hash name, or both curlies
1549to be protected. If both curlies are protected, the result will be compatible
1550with perl4 and perl5. This is a very common practice, and should be changed
1551to use the block form of \f(CW\*(C`eval{}\*(C'\fR if possible.
1552.Sp
1553.Vb 5
1554\& $hashname = "foobar";
1555\& $key = "baz";
1556\& $value = 1234;
1557\& eval "\e$$hashname{'$key'} = q|$value|";
1558\& (defined($foobar{'baz'})) ? (print "Yup") : (print "Nope");
1559.Ve
1560.Sp
1561.Vb 2
1562\& # perl4 prints: Yup
1563\& # perl5 prints: Nope
1564.Ve
1565.Sp
1566Changing
1567.Sp
1568.Vb 1
1569\& eval "\e$$hashname{'$key'} = q|$value|";
1570.Ve
1571.Sp
1572to
1573.Sp
1574.Vb 1
1575\& eval "\e$\e$hashname{'$key'} = q|$value|";
1576.Ve
1577.Sp
1578causes the following result:
1579.Sp
1580.Vb 2
1581\& # perl4 prints: Nope
1582\& # perl5 prints: Yup
1583.Ve
1584.Sp
1585or, changing to
1586.Sp
1587.Vb 1
1588\& eval "\e$$hashname\e{'$key'\e} = q|$value|";
1589.Ve
1590.Sp
1591causes the following result:
1592.Sp
1593.Vb 3
1594\& # perl4 prints: Yup
1595\& # perl5 prints: Yup
1596\& # and is compatible for both versions
1597.Ve
1598.IP "\(bu Interpolation" 5
1599.IX Item "Interpolation"
1600perl4 programs which unconsciously rely on the bugs in earlier perl versions.
1601.Sp
1602.Vb 1
1603\& perl -e '$bar=q/not/; print "This is $foo{$bar} perl5"'
1604.Ve
1605.Sp
1606.Vb 2
1607\& # perl4 prints: This is not perl5
1608\& # perl5 prints: This is perl5
1609.Ve
1610.IP "\(bu Interpolation" 5
1611.IX Item "Interpolation"
1612You also have to be careful about array references.
1613.Sp
1614.Vb 1
1615\& print "$foo{"
1616.Ve
1617.Sp
1618.Vb 2
1619\& perl 4 prints: {
1620\& perl 5 prints: syntax error
1621.Ve
1622.IP "\(bu Interpolation" 5
1623.IX Item "Interpolation"
1624Similarly, watch out for:
1625.Sp
1626.Vb 2
1627\& $foo = "baz";
1628\& print "\e$$foo{bar}\en";
1629.Ve
1630.Sp
1631.Vb 2
1632\& # perl4 prints: $baz{bar}
1633\& # perl5 prints: $
1634.Ve
1635.Sp
1636Perl 5 is looking for \f(CW$foo{bar}\fR which doesn't exist, but perl 4 is
1637happy just to expand \f(CW$foo\fR to \*(L"baz\*(R" by itself. Watch out for this
1638especially in \f(CW\*(C`eval\*(C'\fR's.
1639.IP "\(bu Interpolation" 5
1640.IX Item "Interpolation"
1641\&\f(CW\*(C`qq()\*(C'\fR string passed to \f(CW\*(C`eval\*(C'\fR
1642.Sp
1643.Vb 5
1644\& eval qq(
1645\& foreach \e$y (keys %\e$x\e) {
1646\& \e$count++;
1647\& }
1648\& );
1649.Ve
1650.Sp
1651.Vb 2
1652\& # perl4 runs this ok
1653\& # perl5 prints: Can't find string terminator ")"
1654.Ve
1655.Sh "\s-1DBM\s0 Traps"
1656.IX Subsection "DBM Traps"
1657General \s-1DBM\s0 traps.
1658.IP "\(bu \s-1DBM\s0" 5
1659.IX Item "DBM"
1660Existing dbm databases created under perl4 (or any other dbm/ndbm tool)
1661may cause the same script, run under perl5, to fail. The build of perl5
1662must have been linked with the same dbm/ndbm as the default for \f(CW\*(C`dbmopen()\*(C'\fR
1663to function properly without \f(CW\*(C`tie\*(C'\fR'ing to an extension dbm implementation.
1664.Sp
1665.Vb 2
1666\& dbmopen (%dbm, "file", undef);
1667\& print "ok\en";
1668.Ve
1669.Sp
1670.Vb 2
1671\& # perl4 prints: ok
1672\& # perl5 prints: ok (IFF linked with -ldbm or -lndbm)
1673.Ve
1674.IP "\(bu \s-1DBM\s0" 5
1675.IX Item "DBM"
1676Existing dbm databases created under perl4 (or any other dbm/ndbm tool)
1677may cause the same script, run under perl5, to fail. The error generated
1678when exceeding the limit on the key/value size will cause perl5 to exit
1679immediately.
1680.Sp
1681.Vb 3
1682\& dbmopen(DB, "testdb",0600) || die "couldn't open db! $!";
1683\& $DB{'trap'} = "x" x 1024; # value too large for most dbm/ndbm
1684\& print "YUP\en";
1685.Ve
1686.Sp
1687.Vb 3
1688\& # perl4 prints:
1689\& dbm store returned -1, errno 28, key "trap" at - line 3.
1690\& YUP
1691.Ve
1692.Sp
1693.Vb 2
1694\& # perl5 prints:
1695\& dbm store returned -1, errno 28, key "trap" at - line 3.
1696.Ve
1697.Sh "Unclassified Traps"
1698.IX Subsection "Unclassified Traps"
1699Everything else.
1700.ie n .IP "\(bu ""require""\fR/\f(CW""do"" trap using returned value" 5
1701.el .IP "\(bu \f(CWrequire\fR/\f(CWdo\fR trap using returned value" 5
1702.IX Item "require/do trap using returned value"
1703If the file doit.pl has:
1704.Sp
1705.Vb 5
1706\& sub foo {
1707\& $rc = do "./do.pl";
1708\& return 8;
1709\& }
1710\& print &foo, "\en";
1711.Ve
1712.Sp
1713And the do.pl file has the following single line:
1714.Sp
1715.Vb 1
1716\& return 3;
1717.Ve
1718.Sp
1719Running doit.pl gives the following:
1720.Sp
1721.Vb 2
1722\& # perl 4 prints: 3 (aborts the subroutine early)
1723\& # perl 5 prints: 8
1724.Ve
1725.Sp
1726Same behavior if you replace \f(CW\*(C`do\*(C'\fR with \f(CW\*(C`require\*(C'\fR.
1727.ie n .IP "\(bu ""split"" on empty string with \s-1LIMIT\s0 specified" 5
1728.el .IP "\(bu \f(CWsplit\fR on empty string with \s-1LIMIT\s0 specified" 5
1729.IX Item "split on empty string with LIMIT specified"
1730.Vb 2
1731\& $string = '';
1732\& @list = split(/foo/, $string, 2)
1733.Ve
1734.Sp
1735Perl4 returns a one element list containing the empty string but Perl5
1736returns an empty list.
1737.PP
1738As always, if any of these are ever officially declared as bugs,
1739they'll be fixed and removed.