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