1N/A=head1 NAME
1N/A
1N/Aperltrap - Perl traps for the unwary
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThe biggest trap of all is forgetting to C<use warnings> or use the B<-w>
1N/Aswitch; see L<perllexwarn> and L<perlrun>. The second biggest trap is not
1N/Amaking your entire program runnable under C<use strict>. The third biggest
1N/Atrap is not reading the list of changes in this version of Perl; see
1N/AL<perldelta>.
1N/A
1N/A=head2 Awk Traps
1N/A
1N/AAccustomed B<awk> users should take special note of the following:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AA Perl program executes only once, not once for each input line. You can
1N/Ado an implicit loop with C<-n> or C<-p>.
1N/A
1N/A=item *
1N/A
1N/AThe English module, loaded via
1N/A
1N/A use English;
1N/A
1N/Aallows you to refer to special variables (like C<$/>) with names (like
1N/A$RS), as though they were in B<awk>; see L<perlvar> for details.
1N/A
1N/A=item *
1N/A
1N/ASemicolons are required after all simple statements in Perl (except
1N/Aat the end of a block). Newline is not a statement delimiter.
1N/A
1N/A=item *
1N/A
1N/ACurly brackets are required on C<if>s and C<while>s.
1N/A
1N/A=item *
1N/A
1N/AVariables begin with "$", "@" or "%" in Perl.
1N/A
1N/A=item *
1N/A
1N/AArrays index from 0. Likewise string positions in substr() and
1N/Aindex().
1N/A
1N/A=item *
1N/A
1N/AYou have to decide whether your array has numeric or string indices.
1N/A
1N/A=item *
1N/A
1N/AHash values do not spring into existence upon mere reference.
1N/A
1N/A=item *
1N/A
1N/AYou have to decide whether you want to use string or numeric
1N/Acomparisons.
1N/A
1N/A=item *
1N/A
1N/AReading an input line does not split it for you. You get to split it
1N/Ato an array yourself. And the split() operator has different
1N/Aarguments than B<awk>'s.
1N/A
1N/A=item *
1N/A
1N/AThe current input line is normally in $_, not $0. It generally does
1N/Anot have the newline stripped. ($0 is the name of the program
1N/Aexecuted.) See L<perlvar>.
1N/A
1N/A=item *
1N/A
1N/A$<I<digit>> does not refer to fields--it refers to substrings matched
1N/Aby the last match pattern.
1N/A
1N/A=item *
1N/A
1N/AThe print() statement does not add field and record separators unless
1N/Ayou set C<$,> and C<$\>. You can set $OFS and $ORS if you're using
1N/Athe English module.
1N/A
1N/A=item *
1N/A
1N/AYou must open your files before you print to them.
1N/A
1N/A=item *
1N/A
1N/AThe range operator is "..", not comma. The comma operator works as in
1N/AC.
1N/A
1N/A=item *
1N/A
1N/AThe match operator is "=~", not "~". ("~" is the one's complement
1N/Aoperator, as in C.)
1N/A
1N/A=item *
1N/A
1N/AThe exponentiation operator is "**", not "^". "^" is the XOR
1N/Aoperator, as in C. (You know, one could get the feeling that B<awk> is
1N/Abasically incompatible with C.)
1N/A
1N/A=item *
1N/A
1N/AThe concatenation operator is ".", not the null string. (Using the
1N/Anull string would render C</pat/ /pat/> unparsable, because the third slash
1N/Awould be interpreted as a division operator--the tokenizer is in fact
1N/Aslightly context sensitive for operators like "/", "?", and ">".
1N/AAnd in fact, "." itself can be the beginning of a number.)
1N/A
1N/A=item *
1N/A
1N/AThe C<next>, C<exit>, and C<continue> keywords work differently.
1N/A
1N/A=item *
1N/A
1N/A
1N/AThe following variables work differently:
1N/A
1N/A Awk Perl
1N/A ARGC scalar @ARGV (compare with $#ARGV)
1N/A ARGV[0] $0
1N/A FILENAME $ARGV
1N/A FNR $. - something
1N/A FS (whatever you like)
1N/A NF $#Fld, or some such
1N/A NR $.
1N/A OFMT $#
1N/A OFS $,
1N/A ORS $\
1N/A RLENGTH length($&)
1N/A RS $/
1N/A RSTART length($`)
1N/A SUBSEP $;
1N/A
1N/A=item *
1N/A
1N/AYou cannot set $RS to a pattern, only a string.
1N/A
1N/A=item *
1N/A
1N/AWhen in doubt, run the B<awk> construct through B<a2p> and see what it
1N/Agives you.
1N/A
1N/A=back
1N/A
1N/A=head2 C/C++ Traps
1N/A
1N/ACerebral C and C++ programmers should take note of the following:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/ACurly brackets are required on C<if>'s and C<while>'s.
1N/A
1N/A=item *
1N/A
1N/AYou must use C<elsif> rather than C<else if>.
1N/A
1N/A=item *
1N/A
1N/AThe C<break> and C<continue> keywords from C become in Perl C<last>
1N/Aand C<next>, respectively. Unlike in C, these do I<not> work within a
1N/AC<do { } while> construct. See L<perlsyn/"Loop Control">.
1N/A
1N/A=item *
1N/A
1N/AThere's no switch statement. (But it's easy to build one on the fly,
1N/Asee L<perlsyn/"Basic BLOCKs and Switch Statements">)
1N/A
1N/A=item *
1N/A
1N/AVariables begin with "$", "@" or "%" in Perl.
1N/A
1N/A=item *
1N/A
1N/AComments begin with "#", not "/*" or "//". Perl may interpret C/C++
1N/Acomments as division operators, unterminated regular expressions or
1N/Athe defined-or operator.
1N/A
1N/A=item *
1N/A
1N/AYou can't take the address of anything, although a similar operator
1N/Ain Perl is the backslash, which creates a reference.
1N/A
1N/A=item *
1N/A
1N/AC<ARGV> must be capitalized. C<$ARGV[0]> is C's C<argv[1]>, and C<argv[0]>
1N/Aends up in C<$0>.
1N/A
1N/A=item *
1N/A
1N/ASystem calls such as link(), unlink(), rename(), etc. return nonzero for
1N/Asuccess, not 0. (system(), however, returns zero for success.)
1N/A
1N/A=item *
1N/A
1N/ASignal handlers deal with signal names, not numbers. Use C<kill -l>
1N/Ato find their names on your system.
1N/A
1N/A=back
1N/A
1N/A=head2 Sed Traps
1N/A
1N/ASeasoned B<sed> programmers should take note of the following:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AA Perl program executes only once, not once for each input line. You can
1N/Ado an implicit loop with C<-n> or C<-p>.
1N/A
1N/A=item *
1N/A
1N/ABackreferences in substitutions use "$" rather than "\".
1N/A
1N/A=item *
1N/A
1N/AThe pattern matching metacharacters "(", ")", and "|" do not have backslashes
1N/Ain front.
1N/A
1N/A=item *
1N/A
1N/AThe range operator is C<...>, rather than comma.
1N/A
1N/A=back
1N/A
1N/A=head2 Shell Traps
1N/A
1N/ASharp shell programmers should take note of the following:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AThe backtick operator does variable interpolation without regard to
1N/Athe presence of single quotes in the command.
1N/A
1N/A=item *
1N/A
1N/AThe backtick operator does no translation of the return value, unlike B<csh>.
1N/A
1N/A=item *
1N/A
1N/AShells (especially B<csh>) do several levels of substitution on each
1N/Acommand line. Perl does substitution in only certain constructs
1N/Asuch as double quotes, backticks, angle brackets, and search patterns.
1N/A
1N/A=item *
1N/A
1N/AShells interpret scripts a little bit at a time. Perl compiles the
1N/Aentire program before executing it (except for C<BEGIN> blocks, which
1N/Aexecute at compile time).
1N/A
1N/A=item *
1N/A
1N/AThe arguments are available via @ARGV, not $1, $2, etc.
1N/A
1N/A=item *
1N/A
1N/AThe environment is not automatically made available as separate scalar
1N/Avariables.
1N/A
1N/A=back
1N/A
1N/A=head2 Perl Traps
1N/A
1N/APracticing Perl Programmers should take note of the following:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/ARemember that many operations behave differently in a list
1N/Acontext than they do in a scalar one. See L<perldata> for details.
1N/A
1N/A=item *
1N/A
1N/AAvoid barewords if you can, especially all lowercase ones.
1N/AYou can't tell by just looking at it whether a bareword is
1N/Aa function or a string. By using quotes on strings and
1N/Aparentheses on function calls, you won't ever get them confused.
1N/A
1N/A=item *
1N/A
1N/AYou cannot discern from mere inspection which builtins
1N/Aare unary operators (like chop() and chdir())
1N/Aand which are list operators (like print() and unlink()).
1N/A(Unless prototyped, user-defined subroutines can B<only> be list
1N/Aoperators, never unary ones.) See L<perlop> and L<perlsub>.
1N/A
1N/A=item *
1N/A
1N/APeople have a hard time remembering that some functions
1N/Adefault to $_, or @ARGV, or whatever, but that others which
1N/Ayou might expect to do not.
1N/A
1N/A=item *
1N/A
1N/AThe <FH> construct is not the name of the filehandle, it is a readline
1N/Aoperation on that handle. The data read is assigned to $_ only if the
1N/Afile read is the sole condition in a while loop:
1N/A
1N/A while (<FH>) { }
1N/A while (defined($_ = <FH>)) { }..
1N/A <FH>; # data discarded!
1N/A
1N/A=item *
1N/A
1N/ARemember not to use C<=> when you need C<=~>;
1N/Athese two constructs are quite different:
1N/A
1N/A $x = /foo/;
1N/A $x =~ /foo/;
1N/A
1N/A=item *
1N/A
1N/AThe C<do {}> construct isn't a real loop that you can use
1N/Aloop control on.
1N/A
1N/A=item *
1N/A
1N/AUse C<my()> for local variables whenever you can get away with
1N/Ait (but see L<perlform> for where you can't).
1N/AUsing C<local()> actually gives a local value to a global
1N/Avariable, which leaves you open to unforeseen side-effects
1N/Aof dynamic scoping.
1N/A
1N/A=item *
1N/A
1N/AIf you localize an exported variable in a module, its exported value will
1N/Anot change. The local name becomes an alias to a new value but the
1N/Aexternal name is still an alias for the original.
1N/A
1N/A=back
1N/A
1N/A=head2 Perl4 to Perl5 Traps
1N/A
1N/APracticing Perl4 Programmers should take note of the following
1N/APerl4-to-Perl5 specific traps.
1N/A
1N/AThey're crudely ordered according to the following list:
1N/A
1N/A=over 4
1N/A
1N/A=item Discontinuance, Deprecation, and BugFix traps
1N/A
1N/AAnything that's been fixed as a perl4 bug, removed as a perl4 feature
1N/Aor deprecated as a perl4 feature with the intent to encourage usage of
1N/Asome other perl5 feature.
1N/A
1N/A=item Parsing Traps
1N/A
1N/ATraps that appear to stem from the new parser.
1N/A
1N/A=item Numerical Traps
1N/A
1N/ATraps having to do with numerical or mathematical operators.
1N/A
1N/A=item General data type traps
1N/A
1N/ATraps involving perl standard data types.
1N/A
1N/A=item Context Traps - scalar, list contexts
1N/A
1N/ATraps related to context within lists, scalar statements/declarations.
1N/A
1N/A=item Precedence Traps
1N/A
1N/ATraps related to the precedence of parsing, evaluation, and execution of
1N/Acode.
1N/A
1N/A=item General Regular Expression Traps using s///, etc.
1N/A
1N/ATraps related to the use of pattern matching.
1N/A
1N/A=item Subroutine, Signal, Sorting Traps
1N/A
1N/ATraps related to the use of signals and signal handlers, general subroutines,
1N/Aand sorting, along with sorting subroutines.
1N/A
1N/A=item OS Traps
1N/A
1N/AOS-specific traps.
1N/A
1N/A=item DBM Traps
1N/A
1N/ATraps specific to the use of C<dbmopen()>, and specific dbm implementations.
1N/A
1N/A=item Unclassified Traps
1N/A
1N/AEverything else.
1N/A
1N/A=back
1N/A
1N/AIf you find an example of a conversion trap that is not listed here,
1N/Aplease submit it to <F<perlbug@perl.org>> for inclusion.
1N/AAlso note that at least some of these can be caught with the
1N/AC<use warnings> pragma or the B<-w> switch.
1N/A
1N/A=head2 Discontinuance, Deprecation, and BugFix traps
1N/A
1N/AAnything that has been discontinued, deprecated, or fixed as
1N/Aa bug from perl4.
1N/A
1N/A=over 4
1N/A
1N/A=item * Discontinuance
1N/A
1N/ASymbols starting with "_" are no longer forced into package main, except
1N/Afor C<$_> itself (and C<@_>, etc.).
1N/A
1N/A package test;
1N/A $_legacy = 1;
1N/A
1N/A package main;
1N/A print "\$_legacy is ",$_legacy,"\n";
1N/A
1N/A # perl4 prints: $_legacy is 1
1N/A # perl5 prints: $_legacy is
1N/A
1N/A=item * Deprecation
1N/A
1N/ADouble-colon is now a valid package separator in a variable name. Thus these
1N/Abehave differently in perl4 vs. perl5, because the packages don't exist.
1N/A
1N/A $a=1;$b=2;$c=3;$var=4;
1N/A print "$a::$b::$c ";
1N/A print "$var::abc::xyz\n";
1N/A
1N/A # perl4 prints: 1::2::3 4::abc::xyz
1N/A # perl5 prints: 3
1N/A
1N/AGiven that C<::> is now the preferred package delimiter, it is debatable
1N/Awhether this should be classed as a bug or not.
1N/A(The older package delimiter, ' ,is used here)
1N/A
1N/A $x = 10 ;
1N/A print "x=${'x}\n" ;
1N/A
1N/A # perl4 prints: x=10
1N/A # perl5 prints: Can't find string terminator "'" anywhere before EOF
1N/A
1N/AYou can avoid this problem, and remain compatible with perl4, if you
1N/Aalways explicitly include the package name:
1N/A
1N/A $x = 10 ;
1N/A print "x=${main'x}\n" ;
1N/A
1N/AAlso see precedence traps, for parsing C<$:>.
1N/A
1N/A=item * BugFix
1N/A
1N/AThe second and third arguments of C<splice()> are now evaluated in scalar
1N/Acontext (as the Camel says) rather than list context.
1N/A
1N/A sub sub1{return(0,2) } # return a 2-element list
1N/A sub sub2{ return(1,2,3)} # return a 3-element list
1N/A @a1 = ("a","b","c","d","e");
1N/A @a2 = splice(@a1,&sub1,&sub2);
1N/A print join(' ',@a2),"\n";
1N/A
1N/A # perl4 prints: a b
1N/A # perl5 prints: c d e
1N/A
1N/A=item * Discontinuance
1N/A
1N/AYou can't do a C<goto> into a block that is optimized away. Darn.
1N/A
1N/A goto marker1;
1N/A
1N/A for(1){
1N/A marker1:
1N/A print "Here I is!\n";
1N/A }
1N/A
1N/A # perl4 prints: Here I is!
1N/A # perl5 errors: Can't "goto" into the middle of a foreach loop
1N/A
1N/A=item * Discontinuance
1N/A
1N/AIt is no longer syntactically legal to use whitespace as the name
1N/Aof a variable, or as a delimiter for any kind of quote construct.
1N/ADouble darn.
1N/A
1N/A $a = ("foo bar");
1N/A $b = q baz ;
1N/A print "a is $a, b is $b\n";
1N/A
1N/A # perl4 prints: a is foo bar, b is baz
1N/A # perl5 errors: Bareword found where operator expected
1N/A
1N/A=item * Discontinuance
1N/A
1N/AThe archaic while/if BLOCK BLOCK syntax is no longer supported.
1N/A
1N/A if { 1 } {
1N/A print "True!";
1N/A }
1N/A else {
1N/A print "False!";
1N/A }
1N/A
1N/A # perl4 prints: True!
1N/A # perl5 errors: syntax error at test.pl line 1, near "if {"
1N/A
1N/A=item * BugFix
1N/A
1N/AThe C<**> operator now binds more tightly than unary minus.
1N/AIt was documented to work this way before, but didn't.
1N/A
1N/A print -4**2,"\n";
1N/A
1N/A # perl4 prints: 16
1N/A # perl5 prints: -16
1N/A
1N/A=item * Discontinuance
1N/A
1N/AThe meaning of C<foreach{}> has changed slightly when it is iterating over a
1N/Alist which is not an array. This used to assign the list to a
1N/Atemporary array, but no longer does so (for efficiency). This means
1N/Athat you'll now be iterating over the actual values, not over copies of
1N/Athe values. Modifications to the loop variable can change the original
1N/Avalues.
1N/A
1N/A @list = ('ab','abc','bcd','def');
1N/A foreach $var (grep(/ab/,@list)){
1N/A $var = 1;
1N/A }
1N/A print (join(':',@list));
1N/A
1N/A # perl4 prints: ab:abc:bcd:def
1N/A # perl5 prints: 1:1:bcd:def
1N/A
1N/ATo retain Perl4 semantics you need to assign your list
1N/Aexplicitly to a temporary array and then iterate over that. For
1N/Aexample, you might need to change
1N/A
1N/A foreach $var (grep(/ab/,@list)){
1N/A
1N/Ato
1N/A
1N/A foreach $var (@tmp = grep(/ab/,@list)){
1N/A
1N/AOtherwise changing $var will clobber the values of @list. (This most often
1N/Ahappens when you use C<$_> for the loop variable, and call subroutines in
1N/Athe loop that don't properly localize C<$_>.)
1N/A
1N/A=item * Discontinuance
1N/A
1N/AC<split> with no arguments now behaves like C<split ' '> (which doesn't
1N/Areturn an initial null field if $_ starts with whitespace), it used to
1N/Abehave like C<split /\s+/> (which does).
1N/A
1N/A $_ = ' hi mom';
1N/A print join(':', split);
1N/A
1N/A # perl4 prints: :hi:mom
1N/A # perl5 prints: hi:mom
1N/A
1N/A=item * BugFix
1N/A
1N/APerl 4 would ignore any text which was attached to an B<-e> switch,
1N/Aalways taking the code snippet from the following arg. Additionally, it
1N/Awould silently accept an B<-e> switch without a following arg. Both of
1N/Athese behaviors have been fixed.
1N/A
1N/A perl -e'print "attached to -e"' 'print "separate arg"'
1N/A
1N/A # perl4 prints: separate arg
1N/A # perl5 prints: attached to -e
1N/A
1N/A perl -e
1N/A
1N/A # perl4 prints:
1N/A # perl5 dies: No code specified for -e.
1N/A
1N/A=item * Discontinuance
1N/A
1N/AIn Perl 4 the return value of C<push> was undocumented, but it was
1N/Aactually the last value being pushed onto the target list. In Perl 5
1N/Athe return value of C<push> is documented, but has changed, it is the
1N/Anumber of elements in the resulting list.
1N/A
1N/A @x = ('existing');
1N/A print push(@x, 'first new', 'second new');
1N/A
1N/A # perl4 prints: second new
1N/A # perl5 prints: 3
1N/A
1N/A=item * Deprecation
1N/A
1N/ASome error messages will be different.
1N/A
1N/A=item * Discontinuance
1N/A
1N/AIn Perl 4, if in list context the delimiters to the first argument of
1N/AC<split()> were C<??>, the result would be placed in C<@_> as well as
1N/Abeing returned. Perl 5 has more respect for your subroutine arguments.
1N/A
1N/A=item * Discontinuance
1N/A
1N/ASome bugs may have been inadvertently removed. :-)
1N/A
1N/A=back
1N/A
1N/A=head2 Parsing Traps
1N/A
1N/APerl4-to-Perl5 traps from having to do with parsing.
1N/A
1N/A=over 4
1N/A
1N/A=item * Parsing
1N/A
1N/ANote the space between . and =
1N/A
1N/A $string . = "more string";
1N/A print $string;
1N/A
1N/A # perl4 prints: more string
1N/A # perl5 prints: syntax error at - line 1, near ". ="
1N/A
1N/A=item * Parsing
1N/A
1N/ABetter parsing in perl 5
1N/A
1N/A sub foo {}
1N/A &foo
1N/A print("hello, world\n");
1N/A
1N/A # perl4 prints: hello, world
1N/A # perl5 prints: syntax error
1N/A
1N/A=item * Parsing
1N/A
1N/A"if it looks like a function, it is a function" rule.
1N/A
1N/A print
1N/A ($foo == 1) ? "is one\n" : "is zero\n";
1N/A
1N/A # perl4 prints: is zero
1N/A # perl5 warns: "Useless use of a constant in void context" if using -w
1N/A
1N/A=item * Parsing
1N/A
1N/AString interpolation of the C<$#array> construct differs when braces
1N/Aare to used around the name.
1N/A
1N/A @a = (1..3);
1N/A print "${#a}";
1N/A
1N/A # perl4 prints: 2
1N/A # perl5 fails with syntax error
1N/A
1N/A @ = (1..3);
1N/A print "$#{a}";
1N/A
1N/A # perl4 prints: {a}
1N/A # perl5 prints: 2
1N/A
1N/A=item * Parsing
1N/A
1N/AWhen perl sees C<map {> (or C<grep {>), it has to guess whether the C<{>
1N/Astarts a BLOCK or a hash reference. If it guesses wrong, it will report
1N/Aa syntax error near the C<}> and the missing (or unexpected) comma.
1N/A
1N/AUse unary C<+> before C<{> on a hash reference, and unary C<+> applied
1N/Ato the first thing in a BLOCK (after C<{>), for perl to guess right all
1N/Athe time. (See L<perlfunc/map>.)
1N/A
1N/A=back
1N/A
1N/A=head2 Numerical Traps
1N/A
1N/APerl4-to-Perl5 traps having to do with numerical operators,
1N/Aoperands, or output from same.
1N/A
1N/A=over 5
1N/A
1N/A=item * Numerical
1N/A
1N/AFormatted output and significant digits. In general, Perl 5
1N/Atries to be more precise. For example, on a Solaris Sparc:
1N/A
1N/A print 7.373504 - 0, "\n";
1N/A printf "%20.18f\n", 7.373504 - 0;
1N/A
1N/A # Perl4 prints:
1N/A 7.3750399999999996141
1N/A 7.375039999999999614
1N/A
1N/A # Perl5 prints:
1N/A 7.373504
1N/A 7.375039999999999614
1N/A
1N/ANotice how the first result looks better in Perl 5.
1N/A
1N/AYour results may vary, since your floating point formatting routines
1N/Aand even floating point format may be slightly different.
1N/A
1N/A=item * Numerical
1N/A
1N/AThis specific item has been deleted. It demonstrated how the auto-increment
1N/Aoperator would not catch when a number went over the signed int limit. Fixed
1N/Ain version 5.003_04. But always be wary when using large integers.
1N/AIf in doubt:
1N/A
1N/A use Math::BigInt;
1N/A
1N/A=item * Numerical
1N/A
1N/AAssignment of return values from numeric equality tests
1N/Adoes not work in perl5 when the test evaluates to false (0).
1N/ALogical tests now return a null, instead of 0
1N/A
1N/A $p = ($test == 1);
1N/A print $p,"\n";
1N/A
1N/A # perl4 prints: 0
1N/A # perl5 prints:
1N/A
1N/AAlso see L<"General Regular Expression Traps using s///, etc.">
1N/Afor another example of this new feature...
1N/A
1N/A=item * Bitwise string ops
1N/A
1N/AWhen bitwise operators which can operate upon either numbers or
1N/Astrings (C<& | ^ ~>) are given only strings as arguments, perl4 would
1N/Atreat the operands as bitstrings so long as the program contained a call
1N/Ato the C<vec()> function. perl5 treats the string operands as bitstrings.
1N/A(See L<perlop/Bitwise String Operators> for more details.)
1N/A
1N/A $fred = "10";
1N/A $barney = "12";
1N/A $betty = $fred & $barney;
1N/A print "$betty\n";
1N/A # Uncomment the next line to change perl4's behavior
1N/A # ($dummy) = vec("dummy", 0, 0);
1N/A
1N/A # Perl4 prints:
1N/A 8
1N/A
1N/A # Perl5 prints:
1N/A 10
1N/A
1N/A # If vec() is used anywhere in the program, both print:
1N/A 10
1N/A
1N/A=back
1N/A
1N/A=head2 General data type traps
1N/A
1N/APerl4-to-Perl5 traps involving most data-types, and their usage
1N/Awithin certain expressions and/or context.
1N/A
1N/A=over 5
1N/A
1N/A=item * (Arrays)
1N/A
1N/ANegative array subscripts now count from the end of the array.
1N/A
1N/A @a = (1, 2, 3, 4, 5);
1N/A print "The third element of the array is $a[3] also expressed as $a[-2] \n";
1N/A
1N/A # perl4 prints: The third element of the array is 4 also expressed as
1N/A # perl5 prints: The third element of the array is 4 also expressed as 4
1N/A
1N/A=item * (Arrays)
1N/A
1N/ASetting C<$#array> lower now discards array elements, and makes them
1N/Aimpossible to recover.
1N/A
1N/A @a = (a,b,c,d,e);
1N/A print "Before: ",join('',@a);
1N/A $#a =1;
1N/A print ", After: ",join('',@a);
1N/A $#a =3;
1N/A print ", Recovered: ",join('',@a),"\n";
1N/A
1N/A # perl4 prints: Before: abcde, After: ab, Recovered: abcd
1N/A # perl5 prints: Before: abcde, After: ab, Recovered: ab
1N/A
1N/A=item * (Hashes)
1N/A
1N/AHashes get defined before use
1N/A
1N/A local($s,@a,%h);
1N/A die "scalar \$s defined" if defined($s);
1N/A die "array \@a defined" if defined(@a);
1N/A die "hash \%h defined" if defined(%h);
1N/A
1N/A # perl4 prints:
1N/A # perl5 dies: hash %h defined
1N/A
1N/APerl will now generate a warning when it sees defined(@a) and
1N/Adefined(%h).
1N/A
1N/A=item * (Globs)
1N/A
1N/Aglob assignment from variable to variable will fail if the assigned
1N/Avariable is localized subsequent to the assignment
1N/A
1N/A @a = ("This is Perl 4");
1N/A *b = *a;
1N/A local(@a);
1N/A print @b,"\n";
1N/A
1N/A # perl4 prints: This is Perl 4
1N/A # perl5 prints:
1N/A
1N/A=item * (Globs)
1N/A
1N/AAssigning C<undef> to a glob has no effect in Perl 5. In Perl 4
1N/Ait undefines the associated scalar (but may have other side effects
1N/Aincluding SEGVs). Perl 5 will also warn if C<undef> is assigned to a
1N/Atypeglob. (Note that assigning C<undef> to a typeglob is different
1N/Athan calling the C<undef> function on a typeglob (C<undef *foo>), which
1N/Ahas quite a few effects.
1N/A
1N/A $foo = "bar";
1N/A *foo = undef;
1N/A print $foo;
1N/A
1N/A # perl4 prints:
1N/A # perl4 warns: "Use of uninitialized variable" if using -w
1N/A # perl5 prints: bar
1N/A # perl5 warns: "Undefined value assigned to typeglob" if using -w
1N/A
1N/A=item * (Scalar String)
1N/A
1N/AChanges in unary negation (of strings)
1N/AThis change effects both the return value and what it
1N/Adoes to auto(magic)increment.
1N/A
1N/A $x = "aaa";
1N/A print ++$x," : ";
1N/A print -$x," : ";
1N/A print ++$x,"\n";
1N/A
1N/A # perl4 prints: aab : -0 : 1
1N/A # perl5 prints: aab : -aab : aac
1N/A
1N/A=item * (Constants)
1N/A
1N/Aperl 4 lets you modify constants:
1N/A
1N/A $foo = "x";
1N/A &mod($foo);
1N/A for ($x = 0; $x < 3; $x++) {
1N/A &mod("a");
1N/A }
1N/A sub mod {
1N/A print "before: $_[0]";
1N/A $_[0] = "m";
1N/A print " after: $_[0]\n";
1N/A }
1N/A
1N/A # perl4:
1N/A # before: x after: m
1N/A # before: a after: m
1N/A # before: m after: m
1N/A # before: m after: m
1N/A
1N/A # Perl5:
1N/A # before: x after: m
1N/A # Modification of a read-only value attempted at foo.pl line 12.
1N/A # before: a
1N/A
1N/A=item * (Scalars)
1N/A
1N/AThe behavior is slightly different for:
1N/A
1N/A print "$x", defined $x
1N/A
1N/A # perl 4: 1
1N/A # perl 5: <no output, $x is not called into existence>
1N/A
1N/A=item * (Variable Suicide)
1N/A
1N/AVariable suicide behavior is more consistent under Perl 5.
1N/APerl5 exhibits the same behavior for hashes and scalars,
1N/Athat perl4 exhibits for only scalars.
1N/A
1N/A $aGlobal{ "aKey" } = "global value";
1N/A print "MAIN:", $aGlobal{"aKey"}, "\n";
1N/A $GlobalLevel = 0;
1N/A &test( *aGlobal );
1N/A
1N/A sub test {
1N/A local( *theArgument ) = @_;
1N/A local( %aNewLocal ); # perl 4 != 5.001l,m
1N/A $aNewLocal{"aKey"} = "this should never appear";
1N/A print "SUB: ", $theArgument{"aKey"}, "\n";
1N/A $aNewLocal{"aKey"} = "level $GlobalLevel"; # what should print
1N/A $GlobalLevel++;
1N/A if( $GlobalLevel<4 ) {
1N/A &test( *aNewLocal );
1N/A }
1N/A }
1N/A
1N/A # Perl4:
1N/A # MAIN:global value
1N/A # SUB: global value
1N/A # SUB: level 0
1N/A # SUB: level 1
1N/A # SUB: level 2
1N/A
1N/A # Perl5:
1N/A # MAIN:global value
1N/A # SUB: global value
1N/A # SUB: this should never appear
1N/A # SUB: this should never appear
1N/A # SUB: this should never appear
1N/A
1N/A=back
1N/A
1N/A=head2 Context Traps - scalar, list contexts
1N/A
1N/A=over 5
1N/A
1N/A=item * (list context)
1N/A
1N/AThe elements of argument lists for formats are now evaluated in list
1N/Acontext. This means you can interpolate list values now.
1N/A
1N/A @fmt = ("foo","bar","baz");
1N/A format STDOUT=
1N/A @<<<<< @||||| @>>>>>
1N/A @fmt;
1N/A .
1N/A write;
1N/A
1N/A # perl4 errors: Please use commas to separate fields in file
1N/A # perl5 prints: foo bar baz
1N/A
1N/A=item * (scalar context)
1N/A
1N/AThe C<caller()> function now returns a false value in a scalar context
1N/Aif there is no caller. This lets library files determine if they're
1N/Abeing required.
1N/A
1N/A caller() ? (print "You rang?\n") : (print "Got a 0\n");
1N/A
1N/A # perl4 errors: There is no caller
1N/A # perl5 prints: Got a 0
1N/A
1N/A=item * (scalar context)
1N/A
1N/AThe comma operator in a scalar context is now guaranteed to give a
1N/Ascalar context to its arguments.
1N/A
1N/A @y= ('a','b','c');
1N/A $x = (1, 2, @y);
1N/A print "x = $x\n";
1N/A
1N/A # Perl4 prints: x = c # Thinks list context interpolates list
1N/A # Perl5 prints: x = 3 # Knows scalar uses length of list
1N/A
1N/A=item * (list, builtin)
1N/A
1N/AC<sprintf()> is prototyped as ($;@), so its first argument is given scalar
1N/Acontext. Thus, if passed an array, it will probably not do what you want,
1N/Aunlike Perl 4:
1N/A
1N/A @z = ('%s%s', 'foo', 'bar');
1N/A $x = sprintf(@z);
1N/A print $x;
1N/A
1N/A # perl4 prints: foobar
1N/A # perl5 prints: 3
1N/A
1N/AC<printf()> works the same as it did in Perl 4, though:
1N/A
1N/A @z = ('%s%s', 'foo', 'bar');
1N/A printf STDOUT (@z);
1N/A
1N/A # perl4 prints: foobar
1N/A # perl5 prints: foobar
1N/A
1N/A=back
1N/A
1N/A=head2 Precedence Traps
1N/A
1N/APerl4-to-Perl5 traps involving precedence order.
1N/A
1N/APerl 4 has almost the same precedence rules as Perl 5 for the operators
1N/Athat they both have. Perl 4 however, seems to have had some
1N/Ainconsistencies that made the behavior differ from what was documented.
1N/A
1N/A=over 5
1N/A
1N/A=item * Precedence
1N/A
1N/ALHS vs. RHS of any assignment operator. LHS is evaluated first
1N/Ain perl4, second in perl5; this can affect the relationship
1N/Abetween side-effects in sub-expressions.
1N/A
1N/A @arr = ( 'left', 'right' );
1N/A $a{shift @arr} = shift @arr;
1N/A print join( ' ', keys %a );
1N/A
1N/A # perl4 prints: left
1N/A # perl5 prints: right
1N/A
1N/A=item * Precedence
1N/A
1N/AThese are now semantic errors because of precedence:
1N/A
1N/A @list = (1,2,3,4,5);
1N/A %map = ("a",1,"b",2,"c",3,"d",4);
1N/A $n = shift @list + 2; # first item in list plus 2
1N/A print "n is $n, ";
1N/A $m = keys %map + 2; # number of items in hash plus 2
1N/A print "m is $m\n";
1N/A
1N/A # perl4 prints: n is 3, m is 6
1N/A # perl5 errors and fails to compile
1N/A
1N/A=item * Precedence
1N/A
1N/AThe precedence of assignment operators is now the same as the precedence
1N/Aof assignment. Perl 4 mistakenly gave them the precedence of the associated
1N/Aoperator. So you now must parenthesize them in expressions like
1N/A
1N/A /foo/ ? ($a += 2) : ($a -= 2);
1N/A
1N/AOtherwise
1N/A
1N/A /foo/ ? $a += 2 : $a -= 2
1N/A
1N/Awould be erroneously parsed as
1N/A
1N/A (/foo/ ? $a += 2 : $a) -= 2;
1N/A
1N/AOn the other hand,
1N/A
1N/A $a += /foo/ ? 1 : 2;
1N/A
1N/Anow works as a C programmer would expect.
1N/A
1N/A=item * Precedence
1N/A
1N/A open FOO || die;
1N/A
1N/Ais now incorrect. You need parentheses around the filehandle.
1N/AOtherwise, perl5 leaves the statement as its default precedence:
1N/A
1N/A open(FOO || die);
1N/A
1N/A # perl4 opens or dies
1N/A # perl5 opens FOO, dying only if 'FOO' is false, i.e. never
1N/A
1N/A=item * Precedence
1N/A
1N/Aperl4 gives the special variable, C<$:> precedence, where perl5
1N/Atreats C<$::> as main C<package>
1N/A
1N/A $a = "x"; print "$::a";
1N/A
1N/A # perl 4 prints: -:a
1N/A # perl 5 prints: x
1N/A
1N/A=item * Precedence
1N/A
1N/Aperl4 had buggy precedence for the file test operators vis-a-vis
1N/Athe assignment operators. Thus, although the precedence table
1N/Afor perl4 leads one to believe C<-e $foo .= "q"> should parse as
1N/AC<((-e $foo) .= "q")>, it actually parses as C<(-e ($foo .= "q"))>.
1N/AIn perl5, the precedence is as documented.
1N/A
1N/A -e $foo .= "q"
1N/A
1N/A # perl4 prints: no output
1N/A # perl5 prints: Can't modify -e in concatenation
1N/A
1N/A=item * Precedence
1N/A
1N/AIn perl4, keys(), each() and values() were special high-precedence operators
1N/Athat operated on a single hash, but in perl5, they are regular named unary
1N/Aoperators. As documented, named unary operators have lower precedence
1N/Athan the arithmetic and concatenation operators C<+ - .>, but the perl4
1N/Avariants of these operators actually bind tighter than C<+ - .>.
1N/AThus, for:
1N/A
1N/A %foo = 1..10;
1N/A print keys %foo - 1
1N/A
1N/A # perl4 prints: 4
1N/A # perl5 prints: Type of arg 1 to keys must be hash (not subtraction)
1N/A
1N/AThe perl4 behavior was probably more useful, if less consistent.
1N/A
1N/A=back
1N/A
1N/A=head2 General Regular Expression Traps using s///, etc.
1N/A
1N/AAll types of RE traps.
1N/A
1N/A=over 5
1N/A
1N/A=item * Regular Expression
1N/A
1N/AC<s'$lhs'$rhs'> now does no interpolation on either side. It used to
1N/Ainterpolate $lhs but not $rhs. (And still does not match a literal
1N/A'$' in string)
1N/A
1N/A $a=1;$b=2;
1N/A $string = '1 2 $a $b';
1N/A $string =~ s'$a'$b';
1N/A print $string,"\n";
1N/A
1N/A # perl4 prints: $b 2 $a $b
1N/A # perl5 prints: 1 2 $a $b
1N/A
1N/A=item * Regular Expression
1N/A
1N/AC<m//g> now attaches its state to the searched string rather than the
1N/Aregular expression. (Once the scope of a block is left for the sub, the
1N/Astate of the searched string is lost)
1N/A
1N/A $_ = "ababab";
1N/A while(m/ab/g){
1N/A &doit("blah");
1N/A }
1N/A sub doit{local($_) = shift; print "Got $_ "}
1N/A
1N/A # perl4 prints: Got blah Got blah Got blah Got blah
1N/A # perl5 prints: infinite loop blah...
1N/A
1N/A=item * Regular Expression
1N/A
1N/ACurrently, if you use the C<m//o> qualifier on a regular expression
1N/Awithin an anonymous sub, I<all> closures generated from that anonymous
1N/Asub will use the regular expression as it was compiled when it was used
1N/Athe very first time in any such closure. For instance, if you say
1N/A
1N/A sub build_match {
1N/A my($left,$right) = @_;
1N/A return sub { $_[0] =~ /$left stuff $right/o; };
1N/A }
1N/A $good = build_match('foo','bar');
1N/A $bad = build_match('baz','blarch');
1N/A print $good->('foo stuff bar') ? "ok\n" : "not ok\n";
1N/A print $bad->('baz stuff blarch') ? "ok\n" : "not ok\n";
1N/A print $bad->('foo stuff bar') ? "not ok\n" : "ok\n";
1N/A
1N/AFor most builds of Perl5, this will print:
1N/Aok
1N/Anot ok
1N/Anot ok
1N/A
1N/Abuild_match() will always return a sub which matches the contents of
1N/A$left and $right as they were the I<first> time that build_match()
1N/Awas called, not as they are in the current call.
1N/A
1N/A=item * Regular Expression
1N/A
1N/AIf no parentheses are used in a match, Perl4 sets C<$+> to
1N/Athe whole match, just like C<$&>. Perl5 does not.
1N/A
1N/A "abcdef" =~ /b.*e/;
1N/A print "\$+ = $+\n";
1N/A
1N/A # perl4 prints: bcde
1N/A # perl5 prints:
1N/A
1N/A=item * Regular Expression
1N/A
1N/Asubstitution now returns the null string if it fails
1N/A
1N/A $string = "test";
1N/A $value = ($string =~ s/foo//);
1N/A print $value, "\n";
1N/A
1N/A # perl4 prints: 0
1N/A # perl5 prints:
1N/A
1N/AAlso see L<Numerical Traps> for another example of this new feature.
1N/A
1N/A=item * Regular Expression
1N/A
1N/AC<s`lhs`rhs`> (using backticks) is now a normal substitution, with no
1N/Abacktick expansion
1N/A
1N/A $string = "";
1N/A $string =~ s`^`hostname`;
1N/A print $string, "\n";
1N/A
1N/A # perl4 prints: <the local hostname>
1N/A # perl5 prints: hostname
1N/A
1N/A=item * Regular Expression
1N/A
1N/AStricter parsing of variables used in regular expressions
1N/A
1N/A s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;
1N/A
1N/A # perl4: compiles w/o error
1N/A # perl5: with Scalar found where operator expected ..., near "$opt$plus"
1N/A
1N/Aan added component of this example, apparently from the same script, is
1N/Athe actual value of the s'd string after the substitution.
1N/AC<[$opt]> is a character class in perl4 and an array subscript in perl5
1N/A
1N/A $grpc = 'a';
1N/A $opt = 'r';
1N/A $_ = 'bar';
1N/A s/^([^$grpc]*$grpc[$opt]?)/foo/;
1N/A print ;
1N/A
1N/A # perl4 prints: foo
1N/A # perl5 prints: foobar
1N/A
1N/A=item * Regular Expression
1N/A
1N/AUnder perl5, C<m?x?> matches only once, like C<?x?>. Under perl4, it matched
1N/Arepeatedly, like C</x/> or C<m!x!>.
1N/A
1N/A $test = "once";
1N/A sub match { $test =~ m?once?; }
1N/A &match();
1N/A if( &match() ) {
1N/A # m?x? matches more then once
1N/A print "perl4\n";
1N/A } else {
1N/A # m?x? matches only once
1N/A print "perl5\n";
1N/A }
1N/A
1N/A # perl4 prints: perl4
1N/A # perl5 prints: perl5
1N/A
1N/A=item * Regular Expression
1N/A
1N/AUnlike in Ruby, failed matches in Perl do not reset the match variables
1N/A($1, $2, ..., C<$`>, ...).
1N/A
1N/A=back
1N/A
1N/A=head2 Subroutine, Signal, Sorting Traps
1N/A
1N/AThe general group of Perl4-to-Perl5 traps having to do with
1N/ASignals, Sorting, and their related subroutines, as well as
1N/Ageneral subroutine traps. Includes some OS-Specific traps.
1N/A
1N/A=over 5
1N/A
1N/A=item * (Signals)
1N/A
1N/ABarewords that used to look like strings to Perl will now look like subroutine
1N/Acalls if a subroutine by that name is defined before the compiler sees them.
1N/A
1N/A sub SeeYa { warn"Hasta la vista, baby!" }
1N/A $SIG{'TERM'} = SeeYa;
1N/A print "SIGTERM is now $SIG{'TERM'}\n";
1N/A
1N/A # perl4 prints: SIGTERM is now main'SeeYa
1N/A # perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!")
1N/A
1N/AUse B<-w> to catch this one
1N/A
1N/A=item * (Sort Subroutine)
1N/A
1N/Areverse is no longer allowed as the name of a sort subroutine.
1N/A
1N/A sub reverse{ print "yup "; $a <=> $b }
1N/A print sort reverse (2,1,3);
1N/A
1N/A # perl4 prints: yup yup 123
1N/A # perl5 prints: 123
1N/A # perl5 warns (if using -w): Ambiguous call resolved as CORE::reverse()
1N/A
1N/A=item * warn() won't let you specify a filehandle.
1N/A
1N/AAlthough it _always_ printed to STDERR, warn() would let you specify a
1N/Afilehandle in perl4. With perl5 it does not.
1N/A
1N/A warn STDERR "Foo!";
1N/A
1N/A # perl4 prints: Foo!
1N/A # perl5 prints: String found where operator expected
1N/A
1N/A=back
1N/A
1N/A=head2 OS Traps
1N/A
1N/A=over 5
1N/A
1N/A=item * (SysV)
1N/A
1N/AUnder HPUX, and some other SysV OSes, one had to reset any signal handler,
1N/Awithin the signal handler function, each time a signal was handled with
1N/Aperl4. With perl5, the reset is now done correctly. Any code relying
1N/Aon the handler _not_ being reset will have to be reworked.
1N/A
1N/ASince version 5.002, Perl uses sigaction() under SysV.
1N/A
1N/A sub gotit {
1N/A print "Got @_... ";
1N/A }
1N/A $SIG{'INT'} = 'gotit';
1N/A
1N/A $| = 1;
1N/A $pid = fork;
1N/A if ($pid) {
1N/A kill('INT', $pid);
1N/A sleep(1);
1N/A kill('INT', $pid);
1N/A } else {
1N/A while (1) {sleep(10);}
1N/A }
1N/A
1N/A # perl4 (HPUX) prints: Got INT...
1N/A # perl5 (HPUX) prints: Got INT... Got INT...
1N/A
1N/A=item * (SysV)
1N/A
1N/AUnder SysV OSes, C<seek()> on a file opened to append C<<< >> >>> now does
1N/Athe right thing w.r.t. the fopen() manpage. e.g., - When a file is opened
1N/Afor append, it is impossible to overwrite information already in
1N/Athe file.
1N/A
1N/A open(TEST,">>seek.test");
1N/A $start = tell TEST ;
1N/A foreach(1 .. 9){
1N/A print TEST "$_ ";
1N/A }
1N/A $end = tell TEST ;
1N/A seek(TEST,$start,0);
1N/A print TEST "18 characters here";
1N/A
1N/A # perl4 (solaris) seek.test has: 18 characters here
1N/A # perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here
1N/A
1N/A
1N/A
1N/A=back
1N/A
1N/A=head2 Interpolation Traps
1N/A
1N/APerl4-to-Perl5 traps having to do with how things get interpolated
1N/Awithin certain expressions, statements, contexts, or whatever.
1N/A
1N/A=over 5
1N/A
1N/A=item * Interpolation
1N/A
1N/A@ now always interpolates an array in double-quotish strings.
1N/A
1N/A print "To: someone@somewhere.com\n";
1N/A
1N/A # perl4 prints: To:someone@somewhere.com
1N/A # perl < 5.6.1, error : In string, @somewhere now must be written as \@somewhere
1N/A # perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string
1N/A
1N/A=item * Interpolation
1N/A
1N/ADouble-quoted strings may no longer end with an unescaped $.
1N/A
1N/A $foo = "foo$";
1N/A print "foo is $foo\n";
1N/A
1N/A # perl4 prints: foo is foo$
1N/A # perl5 errors: Final $ should be \$ or $name
1N/A
1N/ANote: perl5 DOES NOT error on the terminating @ in $bar
1N/A
1N/A=item * Interpolation
1N/A
1N/APerl now sometimes evaluates arbitrary expressions inside braces that occur
1N/Awithin double quotes (usually when the opening brace is preceded by C<$>
1N/Aor C<@>).
1N/A
1N/A @www = "buz";
1N/A $foo = "foo";
1N/A $bar = "bar";
1N/A sub foo { return "bar" };
1N/A print "|@{w.w.w}|${main'foo}|";
1N/A
1N/A # perl4 prints: |@{w.w.w}|foo|
1N/A # perl5 prints: |buz|bar|
1N/A
1N/ANote that you can C<use strict;> to ward off such trappiness under perl5.
1N/A
1N/A=item * Interpolation
1N/A
1N/AThe construct "this is $$x" used to interpolate the pid at that point, but
1N/Anow tries to dereference $x. C<$$> by itself still works fine, however.
1N/A
1N/A $s = "a reference";
1N/A $x = *s;
1N/A print "this is $$x\n";
1N/A
1N/A # perl4 prints: this is XXXx (XXX is the current pid)
1N/A # perl5 prints: this is a reference
1N/A
1N/A=item * Interpolation
1N/A
1N/ACreation of hashes on the fly with C<eval "EXPR"> now requires either both
1N/AC<$>'s to be protected in the specification of the hash name, or both curlies
1N/Ato be protected. If both curlies are protected, the result will be compatible
1N/Awith perl4 and perl5. This is a very common practice, and should be changed
1N/Ato use the block form of C<eval{}> if possible.
1N/A
1N/A $hashname = "foobar";
1N/A $key = "baz";
1N/A $value = 1234;
1N/A eval "\$$hashname{'$key'} = q|$value|";
1N/A (defined($foobar{'baz'})) ? (print "Yup") : (print "Nope");
1N/A
1N/A # perl4 prints: Yup
1N/A # perl5 prints: Nope
1N/A
1N/AChanging
1N/A
1N/A eval "\$$hashname{'$key'} = q|$value|";
1N/A
1N/Ato
1N/A
1N/A eval "\$\$hashname{'$key'} = q|$value|";
1N/A
1N/Acauses the following result:
1N/A
1N/A # perl4 prints: Nope
1N/A # perl5 prints: Yup
1N/A
1N/Aor, changing to
1N/A
1N/A eval "\$$hashname\{'$key'\} = q|$value|";
1N/A
1N/Acauses the following result:
1N/A
1N/A # perl4 prints: Yup
1N/A # perl5 prints: Yup
1N/A # and is compatible for both versions
1N/A
1N/A
1N/A=item * Interpolation
1N/A
1N/Aperl4 programs which unconsciously rely on the bugs in earlier perl versions.
1N/A
1N/A perl -e '$bar=q/not/; print "This is $foo{$bar} perl5"'
1N/A
1N/A # perl4 prints: This is not perl5
1N/A # perl5 prints: This is perl5
1N/A
1N/A=item * Interpolation
1N/A
1N/AYou also have to be careful about array and hash brackets during
1N/Ainterpolation.
1N/A
1N/A print "$foo["
1N/A
1N/A perl 4 prints: [
1N/A perl 5 prints: syntax error
1N/A
1N/A print "$foo{"
1N/A
1N/A perl 4 prints: {
1N/A perl 5 prints: syntax error
1N/A
1N/APerl 5 is expecting to find an index or key name following the respective
1N/Abrackets, as well as an ending bracket of the appropriate type. In order
1N/Ato mimic the behavior of Perl 4, you must escape the bracket like so.
1N/A
1N/A print "$foo\[";
1N/A print "$foo\{";
1N/A
1N/A=item * Interpolation
1N/A
1N/ASimilarly, watch out for:
1N/A
1N/A $foo = "baz";
1N/A print "\$$foo{bar}\n";
1N/A
1N/A # perl4 prints: $baz{bar}
1N/A # perl5 prints: $
1N/A
1N/APerl 5 is looking for C<$foo{bar}> which doesn't exist, but perl 4 is
1N/Ahappy just to expand $foo to "baz" by itself. Watch out for this
1N/Aespecially in C<eval>'s.
1N/A
1N/A=item * Interpolation
1N/A
1N/AC<qq()> string passed to C<eval>
1N/A
1N/A eval qq(
1N/A foreach \$y (keys %\$x\) {
1N/A \$count++;
1N/A }
1N/A );
1N/A
1N/A # perl4 runs this ok
1N/A # perl5 prints: Can't find string terminator ")"
1N/A
1N/A=back
1N/A
1N/A=head2 DBM Traps
1N/A
1N/AGeneral DBM traps.
1N/A
1N/A=over 5
1N/A
1N/A=item * DBM
1N/A
1N/AExisting dbm databases created under perl4 (or any other dbm/ndbm tool)
1N/Amay cause the same script, run under perl5, to fail. The build of perl5
1N/Amust have been linked with the same dbm/ndbm as the default for C<dbmopen()>
1N/Ato function properly without C<tie>'ing to an extension dbm implementation.
1N/A
1N/A dbmopen (%dbm, "file", undef);
1N/A print "ok\n";
1N/A
1N/A # perl4 prints: ok
1N/A # perl5 prints: ok (IFF linked with -ldbm or -lndbm)
1N/A
1N/A
1N/A=item * DBM
1N/A
1N/AExisting dbm databases created under perl4 (or any other dbm/ndbm tool)
1N/Amay cause the same script, run under perl5, to fail. The error generated
1N/Awhen exceeding the limit on the key/value size will cause perl5 to exit
1N/Aimmediately.
1N/A
1N/A dbmopen(DB, "testdb",0600) || die "couldn't open db! $!";
1N/A $DB{'trap'} = "x" x 1024; # value too large for most dbm/ndbm
1N/A print "YUP\n";
1N/A
1N/A # perl4 prints:
1N/A dbm store returned -1, errno 28, key "trap" at - line 3.
1N/A YUP
1N/A
1N/A # perl5 prints:
1N/A dbm store returned -1, errno 28, key "trap" at - line 3.
1N/A
1N/A=back
1N/A
1N/A=head2 Unclassified Traps
1N/A
1N/AEverything else.
1N/A
1N/A=over 5
1N/A
1N/A=item * C<require>/C<do> trap using returned value
1N/A
1N/AIf the file doit.pl has:
1N/A
1N/A sub foo {
1N/A $rc = do "./do.pl";
1N/A return 8;
1N/A }
1N/A print &foo, "\n";
1N/A
1N/AAnd the do.pl file has the following single line:
1N/A
1N/A return 3;
1N/A
1N/ARunning doit.pl gives the following:
1N/A
1N/A # perl 4 prints: 3 (aborts the subroutine early)
1N/A # perl 5 prints: 8
1N/A
1N/ASame behavior if you replace C<do> with C<require>.
1N/A
1N/A=item * C<split> on empty string with LIMIT specified
1N/A
1N/A $string = '';
1N/A @list = split(/foo/, $string, 2)
1N/A
1N/APerl4 returns a one element list containing the empty string but Perl5
1N/Areturns an empty list.
1N/A
1N/A=back
1N/A
1N/AAs always, if any of these are ever officially declared as bugs,
1N/Athey'll be fixed and removed.
1N/A