.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.20)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.el \{\
.    de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "PERLTRAP 1"
.TH PERLTRAP 1 "2013-03-04" "perl v5.16.3" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
perltrap \- Perl traps for the unwary
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
The biggest trap of all is forgetting to \f(CW\*(C`use warnings\*(C'\fR or use the \fB\-w\fR
switch; see perllexwarn and perlrun. The second biggest trap is not
making your entire program runnable under \f(CW\*(C`use strict\*(C'\fR.  The third biggest
trap is not reading the list of changes in this version of Perl; see
perldelta.
.SS "Awk Traps"
.IX Subsection "Awk Traps"
Accustomed \fBawk\fR users should take special note of the following:
.IP "\(bu" 4
A Perl program executes only once, not once for each input line.  You can
do an implicit loop with \f(CW\*(C`\-n\*(C'\fR or \f(CW\*(C`\-p\*(C'\fR.
.IP "\(bu" 4
The English module, loaded via
.Sp
.Vb 1
\&    use English;
.Ve
.Sp
allows you to refer to special variables (like \f(CW$/\fR) with names (like
\&\f(CW$RS\fR), as though they were in \fBawk\fR; see perlvar for details.
.IP "\(bu" 4
Semicolons are required after all simple statements in Perl (except
at the end of a block).  Newline is not a statement delimiter.
.IP "\(bu" 4
Curly brackets are required on \f(CW\*(C`if\*(C'\fRs and \f(CW\*(C`while\*(C'\fRs.
.IP "\(bu" 4
Variables begin with \*(L"$\*(R", \*(L"@\*(R" or \*(L"%\*(R" in Perl.
.IP "\(bu" 4
Arrays index from 0.  Likewise string positions in \fIsubstr()\fR and
\&\fIindex()\fR.
.IP "\(bu" 4
You have to decide whether your array has numeric or string indices.
.IP "\(bu" 4
Hash values do not spring into existence upon mere reference.
.IP "\(bu" 4
You have to decide whether you want to use string or numeric
comparisons.
.IP "\(bu" 4
Reading an input line does not split it for you.  You get to split it
to an array yourself.  And the \fIsplit()\fR operator has different
arguments than \fBawk\fR's.
.IP "\(bu" 4
The current input line is normally in \f(CW$_\fR, not \f(CW$0\fR.  It generally does
not have the newline stripped.  ($0 is the name of the program
executed.)  See perlvar.
.IP "\(bu" 4
$<\fIdigit\fR> does not refer to fields\*(--it refers to substrings matched
by the last match pattern.
.IP "\(bu" 4
The \fIprint()\fR statement does not add field and record separators unless
you 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
the English module.
.IP "\(bu" 4
You must open your files before you print to them.
.IP "\(bu" 4
The range operator is \*(L"..\*(R", not comma.  The comma operator works as in
C.
.IP "\(bu" 4
The match operator is \*(L"=~\*(R", not \*(L"~\*(R".  (\*(L"~\*(R" is the one's complement
operator, as in C.)
.IP "\(bu" 4
The exponentiation operator is \*(L"**\*(R", not \*(L"^\*(R".  \*(L"^\*(R" is the \s-1XOR\s0
operator, as in C.  (You know, one could get the feeling that \fBawk\fR is
basically incompatible with C.)
.IP "\(bu" 4
The concatenation operator is \*(L".\*(R", not the null string.  (Using the
null string would render \f(CW\*(C`/pat/ /pat/\*(C'\fR unparsable, because the third slash
would be interpreted as a division operator\*(--the tokenizer is in fact
slightly context sensitive for operators like \*(L"/\*(R", \*(L"?\*(R", and \*(L">\*(R".
And in fact, \*(L".\*(R" itself can be the beginning of a number.)
.IP "\(bu" 4
The \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`exit\*(C'\fR, and \f(CW\*(C`continue\*(C'\fR keywords work differently.
.IP "\(bu" 4
The following variables work differently:
.Sp
.Vb 10
\&      Awk       Perl
\&      ARGC      scalar @ARGV (compare with $#ARGV)
\&      ARGV[0]   $0
\&      FILENAME  $ARGV
\&      FNR       $. \- something
\&      FS        (whatever you like)
\&      NF        $#Fld, or some such
\&      NR        $.
\&      OFMT      $#
\&      OFS       $,
\&      ORS       $\e
\&      RLENGTH   length($&)
\&      RS        $/
\&      RSTART    length($\`)
\&      SUBSEP    $;
.Ve
.IP "\(bu" 4
You cannot set \f(CW$RS\fR to a pattern, only a string.
.IP "\(bu" 4
When in doubt, run the \fBawk\fR construct through \fBa2p\fR and see what it
gives you.
.SS "C/\*(C+ Traps"
.IX Subsection "C/ Traps"
Cerebral C and \*(C+ programmers should take note of the following:
.IP "\(bu" 4
Curly brackets are required on \f(CW\*(C`if\*(C'\fR's and \f(CW\*(C`while\*(C'\fR's.
.IP "\(bu" 4
You must use \f(CW\*(C`elsif\*(C'\fR rather than \f(CW\*(C`else if\*(C'\fR.
.IP "\(bu" 4
The \f(CW\*(C`break\*(C'\fR and \f(CW\*(C`continue\*(C'\fR keywords from C become in Perl \f(CW\*(C`last\*(C'\fR
and \f(CW\*(C`next\*(C'\fR, respectively.  Unlike in C, these do \fInot\fR work within a
\&\f(CW\*(C`do { } while\*(C'\fR construct.  See \*(L"Loop Control\*(R" in perlsyn.
.IP "\(bu" 4
The switch statement is called \f(CW\*(C`given/when\*(C'\fR and only available in
perl 5.10 or newer.  See \*(L"Switch Statements\*(R" in perlsyn.
.IP "\(bu" 4
Variables begin with \*(L"$\*(R", \*(L"@\*(R" or \*(L"%\*(R" in Perl.
.IP "\(bu" 4
Comments begin with \*(L"#\*(R", not \*(L"/*\*(R" or \*(L"//\*(R".  Perl may interpret C/\*(C+
comments as division operators, unterminated regular expressions or
the defined-or operator.
.IP "\(bu" 4
You can't take the address of anything, although a similar operator
in Perl is the backslash, which creates a reference.
.IP "\(bu" 4
\&\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
ends up in \f(CW$0\fR.
.IP "\(bu" 4
System calls such as \fIlink()\fR, \fIunlink()\fR, \fIrename()\fR, etc. return nonzero for
success, not 0. (\fIsystem()\fR, however, returns zero for success.)
.IP "\(bu" 4
Signal handlers deal with signal names, not numbers.  Use \f(CW\*(C`kill \-l\*(C'\fR
to find their names on your system.
.SS "Sed Traps"
.IX Subsection "Sed Traps"
Seasoned \fBsed\fR programmers should take note of the following:
.IP "\(bu" 4
A Perl program executes only once, not once for each input line.  You can
do an implicit loop with \f(CW\*(C`\-n\*(C'\fR or \f(CW\*(C`\-p\*(C'\fR.
.IP "\(bu" 4
Backreferences in substitutions use \*(L"$\*(R" rather than \*(L"\e\*(R".
.IP "\(bu" 4
The pattern matching metacharacters \*(L"(\*(R", \*(L")\*(R", and \*(L"|\*(R" do not have backslashes
in front.
.IP "\(bu" 4
The range operator is \f(CW\*(C`...\*(C'\fR, rather than comma.
.SS "Shell Traps"
.IX Subsection "Shell Traps"
Sharp shell programmers should take note of the following:
.IP "\(bu" 4
The backtick operator does variable interpolation without regard to
the presence of single quotes in the command.
.IP "\(bu" 4
The backtick operator does no translation of the return value, unlike \fBcsh\fR.
.IP "\(bu" 4
Shells (especially \fBcsh\fR) do several levels of substitution on each
command line.  Perl does substitution in only certain constructs
such as double quotes, backticks, angle brackets, and search patterns.
.IP "\(bu" 4
Shells interpret scripts a little bit at a time.  Perl compiles the
entire program before executing it (except for \f(CW\*(C`BEGIN\*(C'\fR blocks, which
execute at compile time).
.IP "\(bu" 4
The arguments are available via \f(CW@ARGV\fR, not \f(CW$1\fR, \f(CW$2\fR, etc.
.IP "\(bu" 4
The environment is not automatically made available as separate scalar
variables.
.IP "\(bu" 4
The shell's \f(CW\*(C`test\*(C'\fR uses \*(L"=\*(R", \*(L"!=\*(R", \*(L"<\*(R" etc for string comparisons and \*(L"\-eq\*(R",
\&\*(L"\-ne\*(R", \*(L"\-lt\*(R" etc for numeric comparisons. This is the reverse of Perl, which
uses \f(CW\*(C`eq\*(C'\fR, \f(CW\*(C`ne\*(C'\fR, \f(CW\*(C`lt\*(C'\fR for string comparisons, and \f(CW\*(C`==\*(C'\fR, \f(CW\*(C`!=\*(C'\fR \f(CW\*(C`<\*(C'\fR etc
for numeric comparisons.
.SS "Perl Traps"
.IX Subsection "Perl Traps"
Practicing Perl Programmers should take note of the following:
.IP "\(bu" 4
Remember that many operations behave differently in a list
context than they do in a scalar one.  See perldata for details.
.IP "\(bu" 4
Avoid barewords if you can, especially all lowercase ones.
You can't tell by just looking at it whether a bareword is
a function or a string.  By using quotes on strings and
parentheses on function calls, you won't ever get them confused.
.IP "\(bu" 4
You cannot discern from mere inspection which builtins
are unary operators (like \fIchop()\fR and \fIchdir()\fR)
and which are list operators (like \fIprint()\fR and \fIunlink()\fR).
(Unless prototyped, user-defined subroutines can \fBonly\fR be list
operators, never unary ones.)  See perlop and perlsub.
.IP "\(bu" 4
People have a hard time remembering that some functions
default to \f(CW$_\fR, or \f(CW@ARGV\fR, or whatever, but that others which
you might expect to do not.
.IP "\(bu" 4
The <\s-1FH\s0> construct is not the name of the filehandle, it is a readline
operation on that handle.  The data read is assigned to \f(CW$_\fR only if the
file read is the sole condition in a while loop:
.Sp
.Vb 3
\&    while (<FH>)      { }
\&    while (defined($_ = <FH>)) { }..
\&    <FH>;  # data discarded!
.Ve
.IP "\(bu" 4
Remember not to use \f(CW\*(C`=\*(C'\fR when you need \f(CW\*(C`=~\*(C'\fR;
these two constructs are quite different:
.Sp
.Vb 2
\&    $x =  /foo/;
\&    $x =~ /foo/;
.Ve
.IP "\(bu" 4
The \f(CW\*(C`do {}\*(C'\fR construct isn't a real loop that you can use
loop control on.
.IP "\(bu" 4
Use \f(CW\*(C`my()\*(C'\fR for local variables whenever you can get away with
it (but see perlform for where you can't).
Using \f(CW\*(C`local()\*(C'\fR actually gives a local value to a global
variable, which leaves you open to unforeseen side-effects
of dynamic scoping.
.IP "\(bu" 4
If you localize an exported variable in a module, its exported value will
not change.  The local name becomes an alias to a new value but the
external name is still an alias for the original.
.SS "Perl4 to Perl5 Traps"
.IX Subsection "Perl4 to Perl5 Traps"
Practicing Perl4 Programmers should take note of the following
Perl4\-to\-Perl5 specific traps.
.PP
They're crudely ordered according to the following list:
.IP "Discontinuance, Deprecation, and BugFix traps" 4
.IX Item "Discontinuance, Deprecation, and BugFix traps"
Anything that's been fixed as a perl4 bug, removed as a perl4 feature
or deprecated as a perl4 feature with the intent to encourage usage of
some other perl5 feature.
.IP "Parsing Traps" 4
.IX Item "Parsing Traps"
Traps that appear to stem from the new parser.
.IP "Numerical Traps" 4
.IX Item "Numerical Traps"
Traps having to do with numerical or mathematical operators.
.IP "General data type traps" 4
.IX Item "General data type traps"
Traps involving perl standard data types.
.IP "Context Traps \- scalar, list contexts" 4
.IX Item "Context Traps - scalar, list contexts"
Traps related to context within lists, scalar statements/declarations.
.IP "Precedence Traps" 4
.IX Item "Precedence Traps"
Traps related to the precedence of parsing, evaluation, and execution of
code.
.IP "General Regular Expression Traps using s///, etc." 4
.IX Item "General Regular Expression Traps using s///, etc."
Traps related to the use of pattern matching.
.IP "Subroutine, Signal, Sorting Traps" 4
.IX Item "Subroutine, Signal, Sorting Traps"
Traps related to the use of signals and signal handlers, general subroutines,
and sorting, along with sorting subroutines.
.IP "\s-1OS\s0 Traps" 4
.IX Item "OS Traps"
OS-specific traps.
.IP "\s-1DBM\s0 Traps" 4
.IX Item "DBM Traps"
Traps specific to the use of \f(CW\*(C`dbmopen()\*(C'\fR, and specific dbm implementations.
.IP "Unclassified Traps" 4
.IX Item "Unclassified Traps"
Everything else.
.PP
If you find an example of a conversion trap that is not listed here,
please submit it to <\fIperlbug@perl.org\fR> for inclusion.
Also note that at least some of these can be caught with the
\&\f(CW\*(C`use warnings\*(C'\fR pragma or the \fB\-w\fR switch.
.SS "Discontinuance, Deprecation, and BugFix traps"
.IX Subsection "Discontinuance, Deprecation, and BugFix traps"
Anything that has been discontinued, deprecated, or fixed as
a bug from perl4.
.IP "\(bu" 4
Symbols starting with \*(L"_\*(R" no longer forced into main
.Sp
Symbols starting with \*(L"_\*(R" are no longer forced into package main, except
for \f(CW$_\fR itself (and \f(CW@_\fR, etc.).
.Sp
.Vb 2
\&    package test;
\&    $_legacy = 1;
\&
\&    package main;
\&    print "\e$_legacy is ",$_legacy,"\en";
\&
\&    # perl4 prints: $_legacy is 1
\&    # perl5 prints: $_legacy is
.Ve
.IP "\(bu" 4
Double-colon valid package separator in variable name
.Sp
Double-colon is now a valid package separator in a variable name.  Thus these
behave differently in perl4 vs. perl5, because the packages don't exist.
.Sp
.Vb 3
\&    $a=1;$b=2;$c=3;$var=4;
\&    print "$a::$b::$c ";
\&    print "$var::abc::xyz\en";
\&
\&    # perl4 prints: 1::2::3 4::abc::xyz
\&    # perl5 prints: 3
.Ve
.Sp
Given that \f(CW\*(C`::\*(C'\fR 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)
.Sp
.Vb 2
\&    $x = 10;
\&    print "x=${\*(Aqx}\en";
\&
\&    # perl4 prints: x=10
\&    # perl5 prints: Can\*(Aqt find string terminator "\*(Aq" anywhere before EOF
.Ve
.Sp
You can avoid this problem, and remain compatible with perl4, if you
always explicitly include the package name:
.Sp
.Vb 2
\&    $x = 10;
\&    print "x=${main\*(Aqx}\en";
.Ve
.Sp
Also see precedence traps, for parsing \f(CW$:\fR.
.IP "\(bu" 4
2nd and 3rd args to \f(CW\*(C`splice()\*(C'\fR are now in scalar context
.Sp
The second and third arguments of \f(CW\*(C`splice()\*(C'\fR are now evaluated in scalar
context (as the Camel says) rather than list context.
.Sp
.Vb 5
\&    sub sub1{return(0,2) }          # return a 2\-element list
\&    sub sub2{ return(1,2,3)}        # return a 3\-element list
\&    @a1 = ("a","b","c","d","e");
\&    @a2 = splice(@a1,&sub1,&sub2);
\&    print join(\*(Aq \*(Aq,@a2),"\en";
\&
\&    # perl4 prints: a b
\&    # perl5 prints: c d e
.Ve
.IP "\(bu" 4
Can't do \f(CW\*(C`goto\*(C'\fR into a block that is optimized away
.Sp
You can't do a \f(CW\*(C`goto\*(C'\fR into a block that is optimized away.  Darn.
.Sp
.Vb 1
\&    goto marker1;
\&
\&    for(1){
\&    marker1:
\&        print "Here I is!\en";
\&    }
\&
\&    # perl4 prints: Here I is!
\&    # perl5 errors: Can\*(Aqt "goto" into the middle of a foreach loop
.Ve
.IP "\(bu" 4
Can't use whitespace as variable name or quote delimiter
.Sp
It is no longer syntactically legal to use whitespace as the name
of a variable, or as a delimiter for any kind of quote construct.
Double darn.
.Sp
.Vb 3
\&    $a = ("foo bar");
\&    $b = q baz ;
\&    print "a is $a, b is $b\en";
\&
\&    # perl4 prints: a is foo bar, b is baz
\&    # perl5 errors: Bareword found where operator expected
.Ve
.IP "\(bu" 4
\&\f(CW\*(C`while/if BLOCK BLOCK\*(C'\fR gone
.Sp
The archaic while/if \s-1BLOCK\s0 \s-1BLOCK\s0 syntax is no longer supported.
.Sp
.Vb 6
\&    if { 1 } {
\&        print "True!";
\&    }
\&    else {
\&        print "False!";
\&    }
\&
\&    # perl4 prints: True!
\&    # perl5 errors: syntax error at test.pl line 1, near "if {"
.Ve
.IP "\(bu" 4
\&\f(CW\*(C`**\*(C'\fR binds tighter than unary minus
.Sp
The \f(CW\*(C`**\*(C'\fR operator now binds more tightly than unary minus.
It was documented to work this way before, but didn't.
.Sp
.Vb 1
\&    print \-4**2,"\en";
\&
\&    # perl4 prints: 16
\&    # perl5 prints: \-16
.Ve
.IP "\(bu" 4
\&\f(CW\*(C`foreach\*(C'\fR changed when iterating over a list
.Sp
The meaning of \f(CW\*(C`foreach{}\*(C'\fR has changed slightly when it is iterating over a
list which is not an array.  This used to assign the list to a
temporary array, but no longer does so (for efficiency).  This means
that you'll now be iterating over the actual values, not over copies of
the values.  Modifications to the loop variable can change the original
values.
.Sp
.Vb 5
\&    @list = (\*(Aqab\*(Aq,\*(Aqabc\*(Aq,\*(Aqbcd\*(Aq,\*(Aqdef\*(Aq);
\&    foreach $var (grep(/ab/,@list)){
\&        $var = 1;
\&    }
\&    print (join(\*(Aq:\*(Aq,@list));
\&
\&    # perl4 prints: ab:abc:bcd:def
\&    # perl5 prints: 1:1:bcd:def
.Ve
.Sp
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
.Sp
.Vb 1
\&    foreach $var (grep(/ab/,@list)){
.Ve
.Sp
to
.Sp
.Vb 1
\&    foreach $var (@tmp = grep(/ab/,@list)){
.Ve
.Sp
Otherwise changing \f(CW$var\fR will clobber the values of \f(CW@list\fR.  (This most often
happens when you use \f(CW$_\fR for the loop variable, and call subroutines in
the loop that don't properly localize \f(CW$_\fR.)
.IP "\(bu" 4
\&\f(CW\*(C`split\*(C'\fR with no args behavior changed
.Sp
\&\f(CW\*(C`split\*(C'\fR with no arguments now behaves like \f(CW\*(C`split \*(Aq \*(Aq\*(C'\fR (which doesn't
return an initial null field if \f(CW$_\fR starts with whitespace), it used to
behave like \f(CW\*(C`split /\es+/\*(C'\fR (which does).
.Sp
.Vb 2
\&    $_ = \*(Aq hi mom\*(Aq;
\&    print join(\*(Aq:\*(Aq, split);
\&
\&    # perl4 prints: :hi:mom
\&    # perl5 prints: hi:mom
.Ve
.IP "\(bu" 4
\&\fB\-e\fR behavior fixed
.Sp
Perl 4 would ignore any text which was attached to an \fB\-e\fR switch,
always taking the code snippet from the following arg.  Additionally, it
would silently accept an \fB\-e\fR switch without a following arg.  Both of
these behaviors have been fixed.
.Sp
.Vb 1
\&    perl \-e\*(Aqprint "attached to \-e"\*(Aq \*(Aqprint "separate arg"\*(Aq
\&
\&    # perl4 prints: separate arg
\&    # perl5 prints: attached to \-e
\&
\&    perl \-e
\&
\&    # perl4 prints:
\&    # perl5 dies: No code specified for \-e.
.Ve
.IP "\(bu" 4
\&\f(CW\*(C`push\*(C'\fR returns number of elements in resulting list
.Sp
In Perl 4 the return value of \f(CW\*(C`push\*(C'\fR was undocumented, but it was
actually the last value being pushed onto the target list.  In Perl 5
the return value of \f(CW\*(C`push\*(C'\fR is documented, but has changed, it is the
number of elements in the resulting list.
.Sp
.Vb 2
\&    @x = (\*(Aqexisting\*(Aq);
\&    print push(@x, \*(Aqfirst new\*(Aq, \*(Aqsecond new\*(Aq);
\&
\&    # perl4 prints: second new
\&    # perl5 prints: 3
.Ve
.IP "\(bu" 4
Some error messages differ
.Sp
Some error messages will be different.
.IP "\(bu" 4
\&\f(CW\*(C`split()\*(C'\fR honors subroutine args
.Sp
In Perl 4, if in list context the delimiters to the first argument of
\&\f(CW\*(C`split()\*(C'\fR were \f(CW\*(C`??\*(C'\fR, the result would be placed in \f(CW@_\fR as well as
being returned.   Perl 5 has more respect for your subroutine arguments.
.IP "\(bu" 4
Bugs removed
.Sp
Some bugs may have been inadvertently removed.  :\-)
.SS "Parsing Traps"
.IX Subsection "Parsing Traps"
Perl4\-to\-Perl5 traps from having to do with parsing.
.IP "\(bu" 4
Space between . and = triggers syntax error
.Sp
Note the space between . and =
.Sp
.Vb 2
\&    $string . = "more string";
\&    print $string;
\&
\&    # perl4 prints: more string
\&    # perl5 prints: syntax error at \- line 1, near ". ="
.Ve
.IP "\(bu" 4
Better parsing in perl 5
.Sp
Better parsing in perl 5
.Sp
.Vb 3
\&    sub foo {}
\&    &foo
\&    print("hello, world\en");
\&
\&    # perl4 prints: hello, world
\&    # perl5 prints: syntax error
.Ve
.IP "\(bu" 4
Function parsing
.Sp
\&\*(L"if it looks like a function, it is a function\*(R" rule.
.Sp
.Vb 2
\&  print
\&    ($foo == 1) ? "is one\en" : "is zero\en";
\&
\&    # perl4 prints: is zero
\&    # perl5 warns: "Useless use of a constant in void context" if using \-w
.Ve
.IP "\(bu" 4
String interpolation of \f(CW$#array\fR differs
.Sp
String interpolation of the \f(CW$#array\fR construct differs when braces
are to used around the name.
.Sp
.Vb 2
\&    @a = (1..3);
\&    print "${#a}";
\&
\&    # perl4 prints: 2
\&    # perl5 fails with syntax error
\&
\&    @a = (1..3);
\&    print "$#{a}";
\&
\&    # perl4 prints: {a}
\&    # perl5 prints: 2
.Ve
.IP "\(bu" 4
Perl guesses on \f(CW\*(C`map\*(C'\fR, \f(CW\*(C`grep\*(C'\fR followed by \f(CW\*(C`{\*(C'\fR if it starts \s-1BLOCK\s0 or hash ref
.Sp
When 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
starts a \s-1BLOCK\s0 or a hash reference. If it guesses wrong, it will report
a syntax error near the \f(CW\*(C`}\*(C'\fR and the missing (or unexpected) comma.
.Sp
Use unary \f(CW\*(C`+\*(C'\fR before \f(CW\*(C`{\*(C'\fR on a hash reference, and unary \f(CW\*(C`+\*(C'\fR applied
to the first thing in a \s-1BLOCK\s0 (after \f(CW\*(C`{\*(C'\fR), for perl to guess right all
the time. (See \*(L"map\*(R" in perlfunc.)
.SS "Numerical Traps"
.IX Subsection "Numerical Traps"
Perl4\-to\-Perl5 traps having to do with numerical operators,
operands, or output from same.
.IP "\(bu" 5
Formatted output and significant digits
.Sp
Formatted output and significant digits.  In general, Perl 5
tries to be more precise.  For example, on a Solaris Sparc:
.Sp
.Vb 2
\&    print 7.373504 \- 0, "\en";
\&    printf "%20.18f\en", 7.373504 \- 0;
\&
\&    # Perl4 prints:
\&    7.3750399999999996141
\&    7.375039999999999614
\&
\&    # Perl5 prints:
\&    7.373504
\&    7.373503999999999614
.Ve
.Sp
Notice how the first result looks better in Perl 5.
.Sp
Your results may vary, since your floating point formatting routines
and even floating point format may be slightly different.
.IP "\(bu" 5
Auto-increment operator over signed int limit deleted
.Sp
This specific item has been deleted.  It demonstrated how the auto-increment
operator would not catch when a number went over the signed int limit.  Fixed
in version 5.003_04.  But always be wary when using large integers.
If in doubt:
.Sp
.Vb 1
\&   use Math::BigInt;
.Ve
.IP "\(bu" 5
Assignment of return values from numeric equality tests doesn't work
.Sp
Assignment of return values from numeric equality tests
does not work in perl5 when the test evaluates to false (0).
Logical tests now return a null, instead of 0
.Sp
.Vb 2
\&    $p = ($test == 1);
\&    print $p,"\en";
\&
\&    # perl4 prints: 0
\&    # perl5 prints:
.Ve
.Sp
Also see \*(L"//, etc.\*(R"\*(L" in \*(R"General Regular Expression Traps using s
for another example of this new feature...
.IP "\(bu" 5
Bitwise string ops
.Sp
When bitwise operators which can operate upon either numbers or
strings (\f(CW\*(C`& | ^ ~\*(C'\fR) are given only strings as arguments, perl4 would
treat the operands as bitstrings so long as the program contained a call
to the \f(CW\*(C`vec()\*(C'\fR function. perl5 treats the string operands as bitstrings.
(See \*(L"Bitwise String Operators\*(R" in perlop for more details.)
.Sp
.Vb 6
\&    $fred = "10";
\&    $barney = "12";
\&    $betty = $fred & $barney;
\&    print "$betty\en";
\&    # Uncomment the next line to change perl4\*(Aqs behavior
\&    # ($dummy) = vec("dummy", 0, 0);
\&
\&    # Perl4 prints:
\&    8
\&
\&    # Perl5 prints:
\&    10
\&
\&    # If vec() is used anywhere in the program, both print:
\&    10
.Ve
.SS "General data type traps"
.IX Subsection "General data type traps"
Perl4\-to\-Perl5 traps involving most data-types, and their usage
within certain expressions and/or context.
.IP "\(bu" 5
Negative array subscripts now count from the end of array
.Sp
Negative array subscripts now count from the end of the array.
.Sp
.Vb 2
\&    @a = (1, 2, 3, 4, 5);
\&    print "The third element of the array is $a[3] also expressed as $a[\-2] \en";
\&
\&    # perl4 prints: The third element of the array is 4 also expressed as
\&    # perl5 prints: The third element of the array is 4 also expressed as 4
.Ve
.IP "\(bu" 5
Setting \f(CW$#array\fR lower now discards array elements
.Sp
Setting \f(CW$#array\fR lower now discards array elements, and makes them
impossible to recover.
.Sp
.Vb 6
\&    @a = (a,b,c,d,e);
\&    print "Before: ",join(\*(Aq\*(Aq,@a);
\&    $#a =1;
\&    print ", After: ",join(\*(Aq\*(Aq,@a);
\&    $#a =3;
\&    print ", Recovered: ",join(\*(Aq\*(Aq,@a),"\en";
\&
\&    # perl4 prints: Before: abcde, After: ab, Recovered: abcd
\&    # perl5 prints: Before: abcde, After: ab, Recovered: ab
.Ve
.IP "\(bu" 5
Hashes get defined before use
.Sp
Hashes get defined before use
.Sp
.Vb 4
\&    local($s,@a,%h);
\&    die "scalar \e$s defined" if defined($s);
\&    die "array \e@a defined" if defined(@a);
\&    die "hash \e%h defined" if defined(%h);
\&
\&    # perl4 prints:
\&    # perl5 dies: hash %h defined
.Ve
.Sp
Perl will now generate a warning when it sees defined(@a) and
defined(%h).
.IP "\(bu" 5
Glob assignment from localized variable to variable
.Sp
glob assignment from variable to variable will fail if the assigned
variable is localized subsequent to the assignment
.Sp
.Vb 4
\&    @a = ("This is Perl 4");
\&    *b = *a;
\&    local(@a);
\&    print @b,"\en";
\&
\&    # perl4 prints: This is Perl 4
\&    # perl5 prints:
.Ve
.IP "\(bu" 5
Assigning \f(CW\*(C`undef\*(C'\fR to glob
.Sp
Assigning \f(CW\*(C`undef\*(C'\fR to a glob has no effect in Perl 5.   In Perl 4
it undefines the associated scalar (but may have other side effects
including SEGVs). Perl 5 will also warn if \f(CW\*(C`undef\*(C'\fR is assigned to a
typeglob. (Note that assigning \f(CW\*(C`undef\*(C'\fR to a typeglob is different
than calling the \f(CW\*(C`undef\*(C'\fR function on a typeglob (\f(CW\*(C`undef *foo\*(C'\fR), which
has quite a few effects.
.Sp
.Vb 3
\&    $foo = "bar";
\&    *foo = undef;
\&    print $foo;
\&
\&    # perl4 prints:
\&    # perl4 warns: "Use of uninitialized variable" if using \-w
\&    # perl5 prints: bar
\&    # perl5 warns: "Undefined value assigned to typeglob" if using \-w
.Ve
.IP "\(bu" 5
Changes in unary negation (of strings)
.Sp
Changes in unary negation (of strings)
This change effects both the return value and what it
does to auto(magic)increment.
.Sp
.Vb 4
\&    $x = "aaa";
\&    print ++$x," : ";
\&    print \-$x," : ";
\&    print ++$x,"\en";
\&
\&    # perl4 prints: aab : \-0 : 1
\&    # perl5 prints: aab : \-aab : aac
.Ve
.IP "\(bu" 5
Modifying of constants prohibited
.Sp
perl 4 lets you modify constants:
.Sp
.Vb 10
\&    $foo = "x";
\&    &mod($foo);
\&    for ($x = 0; $x < 3; $x++) {
\&        &mod("a");
\&    }
\&    sub mod {
\&        print "before: $_[0]";
\&        $_[0] = "m";
\&        print "  after: $_[0]\en";
\&    }
\&
\&    # perl4:
\&    # before: x  after: m
\&    # before: a  after: m
\&    # before: m  after: m
\&    # before: m  after: m
\&
\&    # Perl5:
\&    # before: x  after: m
\&    # Modification of a read\-only value attempted at foo.pl line 12.
\&    # before: a
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`defined $var\*(C'\fR behavior changed
.Sp
The behavior is slightly different for:
.Sp
.Vb 1
\&    print "$x", defined $x
\&
\&    # perl 4: 1
\&    # perl 5: <no output, $x is not called into existence>
.Ve
.IP "\(bu" 5
Variable Suicide
.Sp
Variable suicide behavior is more consistent under Perl 5.
Perl5 exhibits the same behavior for hashes and scalars,
that perl4 exhibits for only scalars.
.Sp
.Vb 4
\&    $aGlobal{ "aKey" } = "global value";
\&    print "MAIN:", $aGlobal{"aKey"}, "\en";
\&    $GlobalLevel = 0;
\&    &test( *aGlobal );
\&
\&    sub test {
\&        local( *theArgument ) = @_;
\&        local( %aNewLocal ); # perl 4 != 5.001l,m
\&        $aNewLocal{"aKey"} = "this should never appear";
\&        print "SUB: ", $theArgument{"aKey"}, "\en";
\&        $aNewLocal{"aKey"} = "level $GlobalLevel";   # what should print
\&        $GlobalLevel++;
\&        if( $GlobalLevel<4 ) {
\&            &test( *aNewLocal );
\&        }
\&    }
\&
\&    # Perl4:
\&    # MAIN:global value
\&    # SUB: global value
\&    # SUB: level 0
\&    # SUB: level 1
\&    # SUB: level 2
\&
\&    # Perl5:
\&    # MAIN:global value
\&    # SUB: global value
\&    # SUB: this should never appear
\&    # SUB: this should never appear
\&    # SUB: this should never appear
.Ve
.SS "Context Traps \- scalar, list contexts"
.IX Subsection "Context Traps - scalar, list contexts"
.IP "\(bu" 5
Elements of argument lists for formats evaluated in list context
.Sp
The elements of argument lists for formats are now evaluated in list
context.  This means you can interpolate list values now.
.Sp
.Vb 6
\&    @fmt = ("foo","bar","baz");
\&    format STDOUT=
\&    @<<<<< @||||| @>>>>>
\&    @fmt;
\&    .
\&    write;
\&
\&    # perl4 errors:  Please use commas to separate fields in file
\&    # perl5 prints: foo     bar      baz
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`caller()\*(C'\fR returns false value in scalar context if no caller present
.Sp
The \f(CW\*(C`caller()\*(C'\fR function now returns a false value in a scalar context
if there is no caller.  This lets library files determine if they're
being required.
.Sp
.Vb 1
\&    caller() ? (print "You rang?\en") : (print "Got a 0\en");
\&
\&    # perl4 errors: There is no caller
\&    # perl5 prints: Got a 0
.Ve
.IP "\(bu" 5
Comma operator in scalar context gives scalar context to args
.Sp
The comma operator in a scalar context is now guaranteed to give a
scalar context to its last argument. It gives scalar or void context
to any preceding arguments, depending on circumstances.
.Sp
.Vb 3
\&    @y= (\*(Aqa\*(Aq,\*(Aqb\*(Aq,\*(Aqc\*(Aq);
\&    $x = (1, 2, @y);
\&    print "x = $x\en";
\&
\&    # Perl4 prints:  x = c   # Interpolates array @y into the list
\&    # Perl5 prints:  x = 3   # Evaluates array @y in scalar context
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`sprintf()\*(C'\fR prototyped as \f(CW\*(C`($;@)\*(C'\fR
.Sp
\&\f(CW\*(C`sprintf()\*(C'\fR is prototyped as ($;@), so its first argument is given scalar
context. Thus, if passed an array, it will probably not do what you want,
unlike Perl 4:
.Sp
.Vb 3
\&    @z = (\*(Aq%s%s\*(Aq, \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq);
\&    $x = sprintf(@z);
\&    print $x;
\&
\&    # perl4 prints: foobar
\&    # perl5 prints: 3
.Ve
.Sp
\&\f(CW\*(C`printf()\*(C'\fR works the same as it did in Perl 4, though:
.Sp
.Vb 2
\&    @z = (\*(Aq%s%s\*(Aq, \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq);
\&    printf STDOUT (@z);
\&
\&    # perl4 prints: foobar
\&    # perl5 prints: foobar
.Ve
.SS "Precedence Traps"
.IX Subsection "Precedence Traps"
Perl4\-to\-Perl5 traps involving precedence order.
.PP
Perl 4 has almost the same precedence rules as Perl 5 for the operators
that they both have.  Perl 4 however, seems to have had some
inconsistencies that made the behavior differ from what was documented.
.IP "\(bu" 5
\&\s-1LHS\s0 vs. \s-1RHS\s0 of any assignment operator
.Sp
\&\s-1LHS\s0 vs. \s-1RHS\s0 of any assignment operator.  \s-1LHS\s0 is evaluated first
in perl4, second in perl5; this can affect the relationship
between side-effects in sub-expressions.
.Sp
.Vb 3
\&    @arr = ( \*(Aqleft\*(Aq, \*(Aqright\*(Aq );
\&    $a{shift @arr} = shift @arr;
\&    print join( \*(Aq \*(Aq, keys %a );
\&
\&    # perl4 prints: left
\&    # perl5 prints: right
.Ve
.IP "\(bu" 5
Semantic errors introduced due to precedence
.Sp
These are now semantic errors because of precedence:
.Sp
.Vb 6
\&    @list = (1,2,3,4,5);
\&    %map = ("a",1,"b",2,"c",3,"d",4);
\&    $n = shift @list + 2;   # first item in list plus 2
\&    print "n is $n, ";
\&    $m = keys %map + 2;     # number of items in hash plus 2
\&    print "m is $m\en";
\&
\&    # perl4 prints: n is 3, m is 6
\&    # perl5 errors and fails to compile
.Ve
.IP "\(bu" 5
Precedence of assignment operators same as the precedence of assignment
.Sp
The precedence of assignment operators is now the same as the precedence
of assignment.  Perl 4 mistakenly gave them the precedence of the associated
operator.  So you now must parenthesize them in expressions like
.Sp
.Vb 1
\&    /foo/ ? ($a += 2) : ($a \-= 2);
.Ve
.Sp
Otherwise
.Sp
.Vb 1
\&    /foo/ ? $a += 2 : $a \-= 2
.Ve
.Sp
would be erroneously parsed as
.Sp
.Vb 1
\&    (/foo/ ? $a += 2 : $a) \-= 2;
.Ve
.Sp
On the other hand,
.Sp
.Vb 1
\&    $a += /foo/ ? 1 : 2;
.Ve
.Sp
now works as a C programmer would expect.
.IP "\(bu" 5
\&\f(CW\*(C`open\*(C'\fR requires parentheses around filehandle
.Sp
.Vb 1
\&    open FOO || die;
.Ve
.Sp
is now incorrect.  You need parentheses around the filehandle.
Otherwise, perl5 leaves the statement as its default precedence:
.Sp
.Vb 1
\&    open(FOO || die);
\&
\&    # perl4 opens or dies
\&    # perl5 opens FOO, dying only if \*(AqFOO\*(Aq is false, i.e. never
.Ve
.IP "\(bu" 5
\&\f(CW$:\fR precedence over \f(CW$::\fR gone
.Sp
perl4 gives the special variable, \f(CW$:\fR precedence, where perl5
treats \f(CW$::\fR as main \f(CW\*(C`package\*(C'\fR
.Sp
.Vb 1
\&    $a = "x"; print "$::a";
\&
\&    # perl 4 prints: \-:a
\&    # perl 5 prints: x
.Ve
.IP "\(bu" 5
Precedence of file test operators documented
.Sp
perl4 had buggy precedence for the file test operators vis-a-vis
the assignment operators.  Thus, although the precedence table
for perl4 leads one to believe \f(CW\*(C`\-e $foo .= "q"\*(C'\fR should parse as
\&\f(CW\*(C`((\-e $foo) .= "q")\*(C'\fR, it actually parses as \f(CW\*(C`(\-e ($foo .= "q"))\*(C'\fR.
In perl5, the precedence is as documented.
.Sp
.Vb 1
\&    \-e $foo .= "q"
\&
\&    # perl4 prints: no output
\&    # perl5 prints: Can\*(Aqt modify \-e in concatenation
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`each\*(C'\fR, \f(CW\*(C`values\*(C'\fR are regular named unary operators
.Sp
In perl4, \fIkeys()\fR, \fIeach()\fR and \fIvalues()\fR were special high-precedence operators
that operated on a single hash, but in perl5, they are regular named unary
operators.  As documented, named unary operators have lower precedence
than the arithmetic and concatenation operators \f(CW\*(C`+ \- .\*(C'\fR, but the perl4
variants of these operators actually bind tighter than \f(CW\*(C`+ \- .\*(C'\fR.
Thus, for:
.Sp
.Vb 2
\&    %foo = 1..10;
\&    print keys %foo \- 1
\&
\&    # perl4 prints: 4
\&    # perl5 prints: Type of arg 1 to keys must be hash (not subtraction)
.Ve
.Sp
The perl4 behavior was probably more useful, if less consistent.
.SS "General Regular Expression Traps using s///, etc."
.IX Subsection "General Regular Expression Traps using s///, etc."
All types of \s-1RE\s0 traps.
.IP "\(bu" 5
\&\f(CW\*(C`s\*(Aq$lhs\*(Aq$rhs\*(Aq\*(C'\fR interpolates on either side
.Sp
\&\f(CW\*(C`s\*(Aq$lhs\*(Aq$rhs\*(Aq\*(C'\fR now does no interpolation on either side.  It used to
interpolate \f(CW$lhs\fR but not \f(CW$rhs\fR.  (And still does not match a literal
\&'$' in string)
.Sp
.Vb 4
\&    $a=1;$b=2;
\&    $string = \*(Aq1 2 $a $b\*(Aq;
\&    $string =~ s\*(Aq$a\*(Aq$b\*(Aq;
\&    print $string,"\en";
\&
\&    # perl4 prints: $b 2 $a $b
\&    # perl5 prints: 1 2 $a $b
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`m//g\*(C'\fR attaches its state to the searched string
.Sp
\&\f(CW\*(C`m//g\*(C'\fR now attaches its state to the searched string rather than the
regular expression.  (Once the scope of a block is left for the sub, the
state of the searched string is lost)
.Sp
.Vb 5
\&    $_ = "ababab";
\&    while(m/ab/g){
\&        &doit("blah");
\&    }
\&    sub doit{local($_) = shift; print "Got $_ "}
\&
\&    # perl4 prints: Got blah Got blah Got blah Got blah
\&    # perl5 prints: infinite loop blah...
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`m//o\*(C'\fR used within an anonymous sub
.Sp
Currently, if you use the \f(CW\*(C`m//o\*(C'\fR qualifier on a regular expression
within an anonymous sub, \fIall\fR closures generated from that anonymous
sub will use the regular expression as it was compiled when it was used
the very first time in any such closure.  For instance, if you say
.Sp
.Vb 9
\&    sub build_match {
\&        my($left,$right) = @_;
\&        return sub { $_[0] =~ /$left stuff $right/o; };
\&    }
\&    $good = build_match(\*(Aqfoo\*(Aq,\*(Aqbar\*(Aq);
\&    $bad = build_match(\*(Aqbaz\*(Aq,\*(Aqblarch\*(Aq);
\&    print $good\->(\*(Aqfoo stuff bar\*(Aq) ? "ok\en" : "not ok\en";
\&    print $bad\->(\*(Aqbaz stuff blarch\*(Aq) ? "ok\en" : "not ok\en";
\&    print $bad\->(\*(Aqfoo stuff bar\*(Aq) ? "not ok\en" : "ok\en";
.Ve
.Sp
For most builds of Perl5, this will print:
ok
not ok
not ok
.Sp
\&\fIbuild_match()\fR will always return a sub which matches the contents of
\&\f(CW$left\fR and \f(CW$right\fR as they were the \fIfirst\fR time that \fIbuild_match()\fR
was called, not as they are in the current call.
.IP "\(bu" 5
\&\f(CW$+\fR isn't set to whole match
.Sp
If no parentheses are used in a match, Perl4 sets \f(CW$+\fR to
the whole match, just like \f(CW$&\fR. Perl5 does not.
.Sp
.Vb 2
\&    "abcdef" =~ /b.*e/;
\&    print "\e$+ = $+\en";
\&
\&    # perl4 prints: bcde
\&    # perl5 prints:
.Ve
.IP "\(bu" 5
Substitution now returns null string if it fails
.Sp
substitution now returns the null string if it fails
.Sp
.Vb 3
\&    $string = "test";
\&    $value = ($string =~ s/foo//);
\&    print $value, "\en";
\&
\&    # perl4 prints: 0
\&    # perl5 prints:
.Ve
.Sp
Also see \*(L"Numerical Traps\*(R" for another example of this new feature.
.IP "\(bu" 5
\&\f(CW\*(C`s\`lhs\`rhs\`\*(C'\fR is now a normal substitution
.Sp
\&\f(CW\*(C`s\`lhs\`rhs\`\*(C'\fR (using backticks) is now a normal substitution, with no
backtick expansion
.Sp
.Vb 3
\&    $string = "";
\&    $string =~ s\`^\`hostname\`;
\&    print $string, "\en";
\&
\&    # perl4 prints: <the local hostname>
\&    # perl5 prints: hostname
.Ve
.IP "\(bu" 5
Stricter parsing of variables in regular expressions
.Sp
Stricter parsing of variables used in regular expressions
.Sp
.Vb 1
\&    s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;
\&
\&    # perl4: compiles w/o error
\&    # perl5: with Scalar found where operator expected ..., near "$opt$plus"
.Ve
.Sp
an added component of this example, apparently from the same script, is
the actual value of the s'd string after the substitution.
\&\f(CW\*(C`[$opt]\*(C'\fR is a character class in perl4 and an array subscript in perl5
.Sp
.Vb 5
\&    $grpc = \*(Aqa\*(Aq;
\&    $opt  = \*(Aqr\*(Aq;
\&    $_ = \*(Aqbar\*(Aq;
\&    s/^([^$grpc]*$grpc[$opt]?)/foo/;
\&    print;
\&
\&    # perl4 prints: foo
\&    # perl5 prints: foobar
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`m?x?\*(C'\fR matches only once
.Sp
Under perl5, \f(CW\*(C`m?x?\*(C'\fR matches only once, like \f(CW\*(C`?x?\*(C'\fR. Under perl4, it matched
repeatedly, like \f(CW\*(C`/x/\*(C'\fR or \f(CW\*(C`m!x!\*(C'\fR.
.Sp
.Vb 10
\&    $test = "once";
\&    sub match { $test =~ m?once?; }
\&    &match();
\&    if( &match() ) {
\&        # m?x? matches more then once
\&        print "perl4\en";
\&    } else {
\&        # m?x? matches only once
\&        print "perl5\en";
\&    }
\&
\&    # perl4 prints: perl4
\&    # perl5 prints: perl5
.Ve
.IP "\(bu" 5
Failed matches don't reset the match variables
.Sp
Unlike in Ruby, failed matches in Perl do not reset the match variables
($1, \f(CW$2\fR, ..., \f(CW\*(C`$\`\*(C'\fR, ...).
.SS "Subroutine, Signal, Sorting Traps"
.IX Subsection "Subroutine, Signal, Sorting Traps"
The general group of Perl4\-to\-Perl5 traps having to do with
Signals, Sorting, and their related subroutines, as well as
general subroutine traps.  Includes some OS-Specific traps.
.IP "\(bu" 5
Barewords that used to look like strings look like subroutine calls
.Sp
Barewords that used to look like strings to Perl will now look like subroutine
calls if a subroutine by that name is defined before the compiler sees them.
.Sp
.Vb 3
\&    sub SeeYa { warn"Hasta la vista, baby!" }
\&    $SIG{\*(AqTERM\*(Aq} = SeeYa;
\&    print "SIGTERM is now $SIG{\*(AqTERM\*(Aq}\en";
\&
\&    # perl4 prints: SIGTERM is now main\*(AqSeeYa
\&    # perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!")
.Ve
.Sp
Use \fB\-w\fR to catch this one
.IP "\(bu" 5
Reverse is no longer allowed as the name of a sort subroutine
.Sp
reverse is no longer allowed as the name of a sort subroutine.
.Sp
.Vb 2
\&    sub reverse{ print "yup "; $a <=> $b }
\&    print sort reverse (2,1,3);
\&
\&    # perl4 prints: yup yup 123
\&    # perl5 prints: 123
\&    # perl5 warns (if using \-w): Ambiguous call resolved as CORE::reverse()
.Ve
.IP "\(bu" 5
\&\f(CW\*(C`warn()\*(C'\fR won't let you specify a filehandle.
.Sp
Although it _always_ printed to \s-1STDERR\s0, \fIwarn()\fR would let you specify a
filehandle in perl4.  With perl5 it does not.
.Sp
.Vb 1
\&    warn STDERR "Foo!";
\&
\&    # perl4 prints: Foo!
\&    # perl5 prints: String found where operator expected
.Ve
.SS "\s-1OS\s0 Traps"
.IX Subsection "OS Traps"
.IP "\(bu" 5
SysV resets signal handler correctly
.Sp
Under \s-1HPUX\s0, and some other SysV OSes, one had to reset any signal handler,
within  the signal handler function, each time a signal was handled with
perl4.  With perl5, the reset is now done correctly.  Any code relying
on the handler _not_ being reset will have to be reworked.
.Sp
Since version 5.002, Perl uses \fIsigaction()\fR under SysV.
.Sp
.Vb 4
\&    sub gotit {
\&        print "Got @_... ";
\&    }
\&    $SIG{\*(AqINT\*(Aq} = \*(Aqgotit\*(Aq;
\&
\&    $| = 1;
\&    $pid = fork;
\&    if ($pid) {
\&        kill(\*(AqINT\*(Aq, $pid);
\&        sleep(1);
\&        kill(\*(AqINT\*(Aq, $pid);
\&    } else {
\&        while (1) {sleep(10);}
\&    }
\&
\&    # perl4 (HPUX) prints: Got INT...
\&    # perl5 (HPUX) prints: Got INT... Got INT...
.Ve
.IP "\(bu" 5
SysV \f(CW\*(C`seek()\*(C'\fR appends correctly
.Sp
Under SysV OSes, \f(CW\*(C`seek()\*(C'\fR on a file opened to append \f(CW\*(C`>>\*(C'\fR now does
the right thing w.r.t. the \fIfopen()\fR manpage. e.g., \- When a file is opened
for append,  it  is  impossible to overwrite information already in
the file.
.Sp
.Vb 8
\&    open(TEST,">>seek.test");
\&    $start = tell TEST;
\&    foreach(1 .. 9){
\&        print TEST "$_ ";
\&    }
\&    $end = tell TEST;
\&    seek(TEST,$start,0);
\&    print TEST "18 characters here";
\&
\&    # perl4 (solaris) seek.test has: 18 characters here
\&    # perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here
.Ve
.SS "Interpolation Traps"
.IX Subsection "Interpolation Traps"
Perl4\-to\-Perl5 traps having to do with how things get interpolated
within certain expressions, statements, contexts, or whatever.
.IP "\(bu" 5
\&\f(CW\*(C`@\*(C'\fR always interpolates an array in double-quotish strings
.Sp
@ now always interpolates an array in double-quotish strings.
.Sp
.Vb 1
\&    print "To: someone@somewhere.com\en";
\&
\&    # perl4 prints: To:someone@somewhere.com
\&    # perl < 5.6.1, error : In string, @somewhere now must be written as \e@somewhere
\&    # perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string
.Ve
.IP "\(bu" 5
Double-quoted strings may no longer end with an unescaped $
.Sp
Double-quoted strings may no longer end with an unescaped $.
.Sp
.Vb 2
\&    $foo = "foo$";
\&    print "foo is $foo\en";
\&
\&    # perl4 prints: foo is foo$
\&    # perl5 errors: Final $ should be \e$ or $name
.Ve
.Sp
Note: perl5 \s-1DOES\s0 \s-1NOT\s0 error on the terminating @ in \f(CW$bar\fR
.IP "\(bu" 5
Arbitrary expressions are evaluated inside braces within double quotes
.Sp
Perl now sometimes evaluates arbitrary expressions inside braces that occur
within double quotes (usually when the opening brace is preceded by \f(CW\*(C`$\*(C'\fR
or \f(CW\*(C`@\*(C'\fR).
.Sp
.Vb 5
\&    @www = "buz";
\&    $foo = "foo";
\&    $bar = "bar";
\&    sub foo { return "bar" };
\&    print "|@{w.w.w}|${main\*(Aqfoo}|";
\&
\&    # perl4 prints: |@{w.w.w}|foo|
\&    # perl5 prints: |buz|bar|
.Ve
.Sp
Note that you can \f(CW\*(C`use strict;\*(C'\fR to ward off such trappiness under perl5.
.IP "\(bu" 5
\&\f(CW$$x\fR now tries to dereference \f(CW$x\fR
.Sp
The construct \*(L"this is $$x\*(R" used to interpolate the pid at that point, but
now tries to dereference \f(CW$x\fR.  \f(CW$$\fR by itself still works fine, however.
.Sp
.Vb 3
\&    $s = "a reference";
\&    $x = *s;
\&    print "this is $$x\en";
\&
\&    # perl4 prints: this is XXXx   (XXX is the current pid)
\&    # perl5 prints: this is a reference
.Ve
.IP "\(bu" 5
Creation of hashes on the fly with \f(CW\*(C`eval "EXPR"\*(C'\fR requires protection
.Sp
Creation of hashes on the fly with \f(CW\*(C`eval "EXPR"\*(C'\fR now requires either both
\&\f(CW\*(C`$\*(C'\fR's to be protected in the specification of the hash name, or both curlies
to be protected.  If both curlies are protected, the result will be compatible
with perl4 and perl5.  This is a very common practice, and should be changed
to use the block form of \f(CW\*(C`eval{}\*(C'\fR  if possible.
.Sp
.Vb 5
\&    $hashname = "foobar";
\&    $key = "baz";
\&    $value = 1234;
\&    eval "\e$$hashname{\*(Aq$key\*(Aq} = q|$value|";
\&    (defined($foobar{\*(Aqbaz\*(Aq})) ?  (print "Yup") : (print "Nope");
\&
\&    # perl4 prints: Yup
\&    # perl5 prints: Nope
.Ve
.Sp
Changing
.Sp
.Vb 1
\&    eval "\e$$hashname{\*(Aq$key\*(Aq} = q|$value|";
.Ve
.Sp
to
.Sp
.Vb 1
\&    eval "\e$\e$hashname{\*(Aq$key\*(Aq} = q|$value|";
.Ve
.Sp
causes the following result:
.Sp
.Vb 2
\&    # perl4 prints: Nope
\&    # perl5 prints: Yup
.Ve
.Sp
or, changing to
.Sp
.Vb 1
\&    eval "\e$$hashname\e{\*(Aq$key\*(Aq\e} = q|$value|";
.Ve
.Sp
causes the following result:
.Sp
.Vb 3
\&    # perl4 prints: Yup
\&    # perl5 prints: Yup
\&    # and is compatible for both versions
.Ve
.IP "\(bu" 5
Bugs in earlier perl versions
.Sp
perl4 programs which unconsciously rely on the bugs in earlier perl versions.
.Sp
.Vb 1
\&    perl \-e \*(Aq$bar=q/not/; print "This is $foo{$bar} perl5"\*(Aq
\&
\&    # perl4 prints: This is not perl5
\&    # perl5 prints: This is perl5
.Ve
.IP "\(bu" 5
Array and hash brackets during interpolation
.Sp
You also have to be careful about array and hash brackets during
interpolation.
.Sp
.Vb 1
\&    print "$foo["
\&
\&    perl 4 prints: [
\&    perl 5 prints: syntax error
\&
\&    print "$foo{"
\&
\&    perl 4 prints: {
\&    perl 5 prints: syntax error
.Ve
.Sp
Perl 5 is expecting to find an index or key name following the respective
brackets, as well as an ending bracket of the appropriate type.  In order
to mimic the behavior of Perl 4, you must escape the bracket like so.
.Sp
.Vb 2
\&    print "$foo\e[";
\&    print "$foo\e{";
.Ve
.IP "\(bu" 5
Interpolation of \f(CW\*(C`\e$$foo{bar}\*(C'\fR
.Sp
Similarly, watch out for: \f(CW\*(C`\e$$foo{bar}\*(C'\fR
.Sp
.Vb 2
\&    $foo = "baz";
\&    print "\e$$foo{bar}\en";
\&
\&    # perl4 prints: $baz{bar}
\&    # perl5 prints: $
.Ve
.Sp
Perl 5 is looking for \f(CW$foo{bar}\fR which doesn't exist, but perl 4 is
happy just to expand \f(CW$foo\fR to \*(L"baz\*(R" by itself.  Watch out for this
especially in \f(CW\*(C`eval\*(C'\fR's.
.IP "\(bu" 5
\&\f(CW\*(C`qq()\*(C'\fR string passed to \f(CW\*(C`eval\*(C'\fR will not find string terminator
.Sp
\&\f(CW\*(C`qq()\*(C'\fR string passed to \f(CW\*(C`eval\*(C'\fR
.Sp
.Vb 5
\&    eval qq(
\&        foreach \e$y (keys %\e$x\e) {
\&            \e$count++;
\&        }
\&    );
\&
\&    # perl4 runs this ok
\&    # perl5 prints: Can\*(Aqt find string terminator ")"
.Ve
.SS "\s-1DBM\s0 Traps"
.IX Subsection "DBM Traps"
General \s-1DBM\s0 traps.
.IP "\(bu" 5
Perl5 must have been linked with same dbm/ndbm as the default for \f(CW\*(C`dbmopen()\*(C'\fR
.Sp
Existing dbm databases created under perl4 (or any other dbm/ndbm tool)
may cause the same script, run under perl5, to fail.  The build of perl5
must have been linked with the same dbm/ndbm as the default for \f(CW\*(C`dbmopen()\*(C'\fR
to function properly without \f(CW\*(C`tie\*(C'\fR'ing to an extension dbm implementation.
.Sp
.Vb 2
\&    dbmopen (%dbm, "file", undef);
\&    print "ok\en";
\&
\&    # perl4 prints: ok
\&    # perl5 prints: ok (IFF linked with \-ldbm or \-lndbm)
.Ve
.IP "\(bu" 5
\&\s-1DBM\s0 exceeding limit on the key/value size will cause perl5 to exit immediately
.Sp
Existing dbm databases created under perl4 (or any other dbm/ndbm tool)
may cause the same script, run under perl5, to fail.  The error generated
when exceeding the limit on the key/value size will cause perl5 to exit
immediately.
.Sp
.Vb 3
\&    dbmopen(DB, "testdb",0600) || die "couldn\*(Aqt open db! $!";
\&    $DB{\*(Aqtrap\*(Aq} = "x" x 1024;  # value too large for most dbm/ndbm
\&    print "YUP\en";
\&
\&    # perl4 prints:
\&    dbm store returned \-1, errno 28, key "trap" at \- line 3.
\&    YUP
\&
\&    # perl5 prints:
\&    dbm store returned \-1, errno 28, key "trap" at \- line 3.
.Ve
.SS "Unclassified Traps"
.IX Subsection "Unclassified Traps"
Everything else.
.IP "\(bu" 5
\&\f(CW\*(C`require\*(C'\fR/\f(CW\*(C`do\*(C'\fR trap using returned value
.Sp
If the file doit.pl has:
.Sp
.Vb 5
\&    sub foo {
\&        $rc = do "./do.pl";
\&        return 8;
\&    }
\&    print &foo, "\en";
.Ve
.Sp
And the do.pl file has the following single line:
.Sp
.Vb 1
\&    return 3;
.Ve
.Sp
Running doit.pl gives the following:
.Sp
.Vb 2
\&    # perl 4 prints: 3 (aborts the subroutine early)
\&    # perl 5 prints: 8
.Ve
.Sp
Same behavior if you replace \f(CW\*(C`do\*(C'\fR with \f(CW\*(C`require\*(C'\fR.
.IP "\(bu" 5
\&\f(CW\*(C`split\*(C'\fR on empty string with \s-1LIMIT\s0 specified
.Sp
.Vb 2
\&    $string = \*(Aq\*(Aq;
\&    @list = split(/foo/, $string, 2)
.Ve
.Sp
Perl4 returns a one element list containing the empty string but Perl5
returns an empty list.
.PP
As always, if any of these are ever officially declared as bugs,
they'll be fixed and removed.
