Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/1perl/perltrap

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.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.  | will give a
.\" real vertical bar.  \*(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-|\(bv\*(Tr
.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\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" 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 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.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.
.Sh "Awk Traps"
.IX Subsection "Awk Traps"
Accustomed \fBawk\fR users should take special note of the following:
.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 15
\&      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.
.Sh "C Traps"
.IX Subsection "C Traps"
Cerebral 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.
.IP "\(bu" 4
There's no switch statement.  (But it's easy to build one on the fly.)
.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".
.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.
.Sh "Sed Traps"
.IX Subsection "Sed Traps"
Seasoned \fBsed\fR programmers should take note of the following:
.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.
.Sh "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.
.Sh "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.
.Sh "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 <\[email protected]\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.
.Sh "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 Discontinuance" 4
.IX Item "Discontinuance"
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;
.Ve
.Sp
.Vb 2
\&    package main;
\&    print "\e$_legacy is ",$_legacy,"\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: $_legacy is 1
\&    # perl5 prints: $_legacy is
.Ve
.IP "\(bu Deprecation" 4
.IX Item "Deprecation"
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";
.Ve
.Sp
.Vb 2
\&    # 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=${'x}\en" ;
.Ve
.Sp
.Vb 2
\&    # perl4 prints: x=10
\&    # perl5 prints: Can't find string terminator "'" 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'x}\en" ;
.Ve
.Sp
Also see precedence traps, for parsing \f(CW$:\fR.
.IP "\(bu BugFix" 4
.IX Item "BugFix"
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(' ',@a2),"\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: a b
\&    # perl5 prints: c d e
.Ve
.IP "\(bu Discontinuance" 4
.IX Item "Discontinuance"
You can't do a \f(CW\*(C`goto\*(C'\fR into a block that is optimized away.  Darn.
.Sp
.Vb 1
\&    goto marker1;
.Ve
.Sp
.Vb 4
\&    for(1){
\&    marker1:
\&        print "Here I is!\en";
\&    }
.Ve
.Sp
.Vb 2
\&    # perl4 prints: Here I is!
\&    # perl5 errors: Can't "goto" into the middle of a foreach loop
.Ve
.IP "\(bu Discontinuance" 4
.IX Item "Discontinuance"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: a is foo bar, b is baz
\&    # perl5 errors: Bareword found where operator expected
.Ve
.IP "\(bu Discontinuance" 4
.IX Item "Discontinuance"
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!";
\&    }
.Ve
.Sp
.Vb 2
\&    # perl4 prints: True!
\&    # perl5 errors: syntax error at test.pl line 1, near "if {"
.Ve
.IP "\(bu BugFix" 4
.IX Item "BugFix"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: 16
\&    # perl5 prints: -16
.Ve
.IP "\(bu Discontinuance" 4
.IX Item "Discontinuance"
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 = ('ab','abc','bcd','def');
\&    foreach $var (grep(/ab/,@list)){
\&        $var = 1;
\&    }
\&    print (join(':',@list));
.Ve
.Sp
.Vb 2
\&    # 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 Discontinuance" 4
.IX Item "Discontinuance"
\&\f(CW\*(C`split\*(C'\fR with no arguments now behaves like \f(CW\*(C`split ' '\*(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
\&    $_ = ' hi mom';
\&    print join(':', split);
.Ve
.Sp
.Vb 2
\&    # perl4 prints: :hi:mom
\&    # perl5 prints: hi:mom
.Ve
.IP "\(bu BugFix" 4
.IX Item "BugFix"
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'print "attached to -e"' 'print "separate arg"'
.Ve
.Sp
.Vb 2
\&    # perl4 prints: separate arg
\&    # perl5 prints: attached to -e
.Ve
.Sp
.Vb 1
\&    perl -e
.Ve
.Sp
.Vb 2
\&    # perl4 prints:
\&    # perl5 dies: No code specified for -e.
.Ve
.IP "\(bu Discontinuance" 4
.IX Item "Discontinuance"
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 = ('existing');
\&    print push(@x, 'first new', 'second new');
.Ve
.Sp
.Vb 2
\&    # perl4 prints: second new
\&    # perl5 prints: 3
.Ve
.IP "\(bu Deprecation" 4
.IX Item "Deprecation"
Some error messages will be different.
.IP "\(bu Discontinuance" 4
.IX Item "Discontinuance"
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 Discontinuance" 4
.IX Item "Discontinuance"
Some bugs may have been inadvertently removed.  :\-)
.Sh "Parsing Traps"
.IX Subsection "Parsing Traps"
Perl4\-to\-Perl5 traps from having to do with parsing.
.IP "\(bu Parsing" 4
.IX Item "Parsing"
Note the space between . and =
.Sp
.Vb 2
\&    $string . = "more string";
\&    print $string;
.Ve
.Sp
.Vb 2
\&    # perl4 prints: more string
\&    # perl5 prints: syntax error at - line 1, near ". ="
.Ve
.IP "\(bu Parsing" 4
.IX Item "Parsing"
Better parsing in perl 5
.Sp
.Vb 3
\&    sub foo {}
\&    &foo
\&    print("hello, world\en");
.Ve
.Sp
.Vb 2
\&    # perl4 prints: hello, world
\&    # perl5 prints: syntax error
.Ve
.IP "\(bu Parsing" 4
.IX Item "Parsing"
\&\*(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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: is zero
\&    # perl5 warns: "Useless use of a constant in void context" if using -w
.Ve
.IP "\(bu Parsing" 4
.IX Item "Parsing"
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}";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: 2
\&    # perl5 fails with syntax error
.Ve
.Sp
.Vb 2
\&    @ = (1..3);
\&    print "$#{a}";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: {a}
\&    # perl5 prints: 2
.Ve
.IP "\(bu Parsing" 4
.IX Item "Parsing"
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.)
.Sh "Numerical Traps"
.IX Subsection "Numerical Traps"
Perl4\-to\-Perl5 traps having to do with numerical operators,
operands, or output from same.
.IP "\(bu Numerical" 5
.IX Item "Numerical"
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;
.Ve
.Sp
.Vb 3
\&    # Perl4 prints:
\&    7.3750399999999996141
\&    7.375039999999999614
.Ve
.Sp
.Vb 3
\&    # Perl5 prints:
\&    7.373504
\&    7.375039999999999614
.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 Numerical" 5
.IX Item "Numerical"
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 Numerical" 5
.IX Item "Numerical"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: 0
\&    # perl5 prints:
.Ve
.Sp
Also see \*(L"General Regular Expression Traps using s///, etc.\*(R"
for another example of this new feature...
.IP "\(bu Bitwise string ops" 5
.IX Item "Bitwise string ops"
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's behavior
\&    # ($dummy) = vec("dummy", 0, 0);
.Ve
.Sp
.Vb 2
\&    # Perl4 prints:
\&    8
.Ve
.Sp
.Vb 2
\&    # Perl5 prints:
\&    10
.Ve
.Sp
.Vb 2
\&    # If vec() is used anywhere in the program, both print:
\&    10
.Ve
.Sh "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 (Arrays)" 5
.IX Item "(Arrays)"
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";
.Ve
.Sp
.Vb 2
\&    # 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 (Arrays)" 5
.IX Item "(Arrays)"
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('',@a);
\&    $#a =1;
\&    print ", After: ",join('',@a);
\&    $#a =3;
\&    print ", Recovered: ",join('',@a),"\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: Before: abcde, After: ab, Recovered: abcd
\&    # perl5 prints: Before: abcde, After: ab, Recovered: ab
.Ve
.IP "\(bu (Hashes)" 5
.IX Item "(Hashes)"
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);
.Ve
.Sp
.Vb 2
\&    # 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 (Globs)" 5
.IX Item "(Globs)"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: This is Perl 4
\&    # perl5 prints:
.Ve
.IP "\(bu (Globs)" 5
.IX Item "(Globs)"
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;
.Ve
.Sp
.Vb 4
\&    # 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 (Scalar String)" 5
.IX Item "(Scalar String)"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: aab : -0 : 1
\&    # perl5 prints: aab : -aab : aac
.Ve
.IP "\(bu (Constants)" 5
.IX Item "(Constants)"
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";
\&    }
.Ve
.Sp
.Vb 5
\&    # perl4:
\&    # before: x  after: m
\&    # before: a  after: m
\&    # before: m  after: m
\&    # before: m  after: m
.Ve
.Sp
.Vb 4
\&    # Perl5:
\&    # before: x  after: m
\&    # Modification of a read-only value attempted at foo.pl line 12.
\&    # before: a
.Ve
.IP "\(bu (Scalars)" 5
.IX Item "(Scalars)"
The behavior is slightly different for:
.Sp
.Vb 1
\&    print "$x", defined $x
.Ve
.Sp
.Vb 2
\&    # perl 4: 1
\&    # perl 5: <no output, $x is not called into existence>
.Ve
.IP "\(bu (Variable Suicide)" 5
.IX Item "(Variable Suicide)"
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 );
.Ve
.Sp
.Vb 11
\&    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 );
\&        }
\&    }
.Ve
.Sp
.Vb 6
\&    # Perl4:
\&    # MAIN:global value
\&    # SUB: global value
\&    # SUB: level 0
\&    # SUB: level 1
\&    # SUB: level 2
.Ve
.Sp
.Vb 6
\&    # Perl5:
\&    # MAIN:global value
\&    # SUB: global value
\&    # SUB: this should never appear
\&    # SUB: this should never appear
\&    # SUB: this should never appear
.Ve
.Sh "Context Traps \- scalar, list contexts"
.IX Subsection "Context Traps - scalar, list contexts"
.IP "\(bu (list context)" 5
.IX Item "(list context)"
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;
.Ve
.Sp
.Vb 2
\&    # perl4 errors:  Please use commas to separate fields in file
\&    # perl5 prints: foo     bar      baz
.Ve
.IP "\(bu (scalar context)" 5
.IX Item "(scalar context)"
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");
.Ve
.Sp
.Vb 2
\&    # perl4 errors: There is no caller
\&    # perl5 prints: Got a 0
.Ve
.IP "\(bu (scalar context)" 5
.IX Item "(scalar context)"
The comma operator in a scalar context is now guaranteed to give a
scalar context to its arguments.
.Sp
.Vb 3
\&    @y= ('a','b','c');
\&    $x = (1, 2, @y);
\&    print "x = $x\en";
.Ve
.Sp
.Vb 2
\&    # Perl4 prints:  x = c   # Thinks list context interpolates list
\&    # Perl5 prints:  x = 3   # Knows scalar uses length of list
.Ve
.IP "\(bu (list, builtin)" 5
.IX Item "(list, builtin)"
\&\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 = ('%s%s', 'foo', 'bar');
\&    $x = sprintf(@z);
\&    print $x;
.Ve
.Sp
.Vb 2
\&    # 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 = ('%s%s', 'foo', 'bar');
\&    printf STDOUT (@z);
.Ve
.Sp
.Vb 2
\&    # perl4 prints: foobar
\&    # perl5 prints: foobar
.Ve
.Sh "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 Precedence" 5
.IX Item "Precedence"
\&\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 = ( 'left', 'right' );
\&    $a{shift @arr} = shift @arr;
\&    print join( ' ', keys %a );
.Ve
.Sp
.Vb 2
\&    # perl4 prints: left
\&    # perl5 prints: right
.Ve
.IP "\(bu Precedence" 5
.IX Item "Precedence"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: n is 3, m is 6
\&    # perl5 errors and fails to compile
.Ve
.IP "\(bu Precedence" 5
.IX Item "Precedence"
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 Precedence" 5
.IX Item "Precedence"
.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);
.Ve
.Sp
.Vb 2
\&    # perl4 opens or dies
\&    # perl5 opens FOO, dying only if 'FOO' is false, i.e. never
.Ve
.IP "\(bu Precedence" 5
.IX Item "Precedence"
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";
.Ve
.Sp
.Vb 2
\&    # perl 4 prints: -:a
\&    # perl 5 prints: x
.Ve
.IP "\(bu Precedence" 5
.IX Item "Precedence"
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"
.Ve
.Sp
.Vb 2
\&    # perl4 prints: no output
\&    # perl5 prints: Can't modify -e in concatenation
.Ve
.IP "\(bu Precedence" 5
.IX Item "Precedence"
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
.Ve
.Sp
.Vb 2
\&    # 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.
.Sh "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 Regular Expression" 5
.IX Item "Regular Expression"
\&\f(CW\*(C`s'$lhs'$rhs'\*(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 = '1 2 $a $b';
\&    $string =~ s'$a'$b';
\&    print $string,"\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: $b 2 $a $b
\&    # perl5 prints: 1 2 $a $b
.Ve
.IP "\(bu Regular Expression" 5
.IX Item "Regular Expression"
\&\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 $_ "}
.Ve
.Sp
.Vb 2
\&    # perl4 prints: Got blah Got blah Got blah Got blah
\&    # perl5 prints: infinite loop blah...
.Ve
.IP "\(bu Regular Expression" 5
.IX Item "Regular Expression"
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('foo','bar');
\&    $bad = build_match('baz','blarch');
\&    print $good->('foo stuff bar') ? "ok\en" : "not ok\en";
\&    print $bad->('baz stuff blarch') ? "ok\en" : "not ok\en";
\&    print $bad->('foo stuff bar') ? "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 Regular Expression" 5
.IX Item "Regular Expression"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: bcde
\&    # perl5 prints:
.Ve
.IP "\(bu Regular Expression" 5
.IX Item "Regular Expression"
substitution now returns the null string if it fails
.Sp
.Vb 3
\&    $string = "test";
\&    $value = ($string =~ s/foo//);
\&    print $value, "\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: 0
\&    # perl5 prints:
.Ve
.Sp
Also see \*(L"Numerical Traps\*(R" for another example of this new feature.
.IP "\(bu Regular Expression" 5
.IX Item "Regular Expression"
\&\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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: <the local hostname>
\&    # perl5 prints: hostname
.Ve
.IP "\(bu Regular Expression" 5
.IX Item "Regular Expression"
Stricter parsing of variables used in regular expressions
.Sp
.Vb 1
\&    s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o;
.Ve
.Sp
.Vb 2
\&    # 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 = 'a';
\&    $opt  = 'r';
\&    $_ = 'bar';
\&    s/^([^$grpc]*$grpc[$opt]?)/foo/;
\&    print ;
.Ve
.Sp
.Vb 2
\&    # perl4 prints: foo
\&    # perl5 prints: foobar
.Ve
.IP "\(bu Regular Expression" 5
.IX Item "Regular Expression"
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";
\&    }
.Ve
.Sp
.Vb 2
\&    # perl4 prints: perl4
\&    # perl5 prints: perl5
.Ve
.Sh "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 (Signals)" 5
.IX Item "(Signals)"
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{'TERM'} = SeeYa;
\&    print "SIGTERM is now $SIG{'TERM'}\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: SIGTERM is now main'SeeYa
\&    # 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 (Sort Subroutine)" 5
.IX Item "(Sort Subroutine)"
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);
.Ve
.Sp
.Vb 3
\&    # perl4 prints: yup yup 123
\&    # perl5 prints: 123
\&    # perl5 warns (if using -w): Ambiguous call resolved as CORE::reverse()
.Ve
.IP "\(bu \fIwarn()\fR won't let you specify a filehandle." 5
.IX Item "warn() won't let you specify a filehandle."
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!";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: Foo!
\&    # perl5 prints: String found where operator expected
.Ve
.Sh "\s-1OS\s0 Traps"
.IX Subsection "OS Traps"
.IP "\(bu (SysV)" 5
.IX Item "(SysV)"
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{'INT'} = 'gotit';
.Ve
.Sp
.Vb 9
\&    $| = 1;
\&    $pid = fork;
\&    if ($pid) {
\&        kill('INT', $pid);
\&        sleep(1);
\&        kill('INT', $pid);
\&    } else {
\&        while (1) {sleep(10);}
\&    }
.Ve
.Sp
.Vb 2
\&    # perl4 (HPUX) prints: Got INT...
\&    # perl5 (HPUX) prints: Got INT... Got INT...
.Ve
.IP "\(bu (SysV)" 5
.IX Item "(SysV)"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 (solaris) seek.test has: 18 characters here
\&    # perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here
.Ve
.Sh "Interpolation Traps"
.IX Subsection "Interpolation Traps"
Perl4\-to\-Perl5 traps having to do with how things get interpolated
within certain expressions, statements, contexts, or whatever.
.IP "\(bu Interpolation" 5
.IX Item "Interpolation"
@ now always interpolates an array in double-quotish strings.
.Sp
.Vb 1
\&    print "To: [email protected]\en";
.Ve
.Sp
.Vb 3
\&    # perl4 prints: To:[email protected]
\&    # 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 Interpolation" 5
.IX Item "Interpolation"
Double-quoted strings may no longer end with an unescaped $ or @.
.Sp
.Vb 3
\&    $foo = "foo$";
\&    $bar = "bar@";
\&    print "foo is $foo, bar is $bar\en";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: foo is foo$, bar is bar@
\&    # 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 Interpolation" 5
.IX Item "Interpolation"
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'foo}|";
.Ve
.Sp
.Vb 2
\&    # 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 Interpolation" 5
.IX Item "Interpolation"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: this is XXXx   (XXX is the current pid)
\&    # perl5 prints: this is a reference
.Ve
.IP "\(bu Interpolation" 5
.IX Item "Interpolation"
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{'$key'} = q|$value|";
\&    (defined($foobar{'baz'})) ?  (print "Yup") : (print "Nope");
.Ve
.Sp
.Vb 2
\&    # perl4 prints: Yup
\&    # perl5 prints: Nope
.Ve
.Sp
Changing
.Sp
.Vb 1
\&    eval "\e$$hashname{'$key'} = q|$value|";
.Ve
.Sp
to
.Sp
.Vb 1
\&    eval "\e$\e$hashname{'$key'} = 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{'$key'\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 Interpolation" 5
.IX Item "Interpolation"
perl4 programs which unconsciously rely on the bugs in earlier perl versions.
.Sp
.Vb 1
\&    perl -e '$bar=q/not/; print "This is $foo{$bar} perl5"'
.Ve
.Sp
.Vb 2
\&    # perl4 prints: This is not perl5
\&    # perl5 prints: This is perl5
.Ve
.IP "\(bu Interpolation" 5
.IX Item "Interpolation"
You also have to be careful about array references.
.Sp
.Vb 1
\&    print "$foo{"
.Ve
.Sp
.Vb 2
\&    perl 4 prints: {
\&    perl 5 prints: syntax error
.Ve
.IP "\(bu Interpolation" 5
.IX Item "Interpolation"
Similarly, watch out for:
.Sp
.Vb 2
\&    $foo = "baz";
\&    print "\e$$foo{bar}\en";
.Ve
.Sp
.Vb 2
\&    # 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 Interpolation" 5
.IX Item "Interpolation"
\&\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++;
\&        }
\&    );
.Ve
.Sp
.Vb 2
\&    # perl4 runs this ok
\&    # perl5 prints: Can't find string terminator ")"
.Ve
.Sh "\s-1DBM\s0 Traps"
.IX Subsection "DBM Traps"
General \s-1DBM\s0 traps.
.IP "\(bu \s-1DBM\s0" 5
.IX Item "DBM"
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";
.Ve
.Sp
.Vb 2
\&    # perl4 prints: ok
\&    # perl5 prints: ok (IFF linked with -ldbm or -lndbm)
.Ve
.IP "\(bu \s-1DBM\s0" 5
.IX Item "DBM"
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't open db! $!";
\&    $DB{'trap'} = "x" x 1024;  # value too large for most dbm/ndbm
\&    print "YUP\en";
.Ve
.Sp
.Vb 3
\&    # perl4 prints:
\&    dbm store returned -1, errno 28, key "trap" at - line 3.
\&    YUP
.Ve
.Sp
.Vb 2
\&    # perl5 prints:
\&    dbm store returned -1, errno 28, key "trap" at - line 3.
.Ve
.Sh "Unclassified Traps"
.IX Subsection "Unclassified Traps"
Everything else.
.ie n .IP "\(bu ""require""\fR/\f(CW""do"" trap using returned value" 5
.el .IP "\(bu \f(CWrequire\fR/\f(CWdo\fR trap using returned value" 5
.IX Item "require/do trap using returned value"
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.
.ie n .IP "\(bu ""split"" on empty string with \s-1LIMIT\s0 specified" 5
.el .IP "\(bu \f(CWsplit\fR on empty string with \s-1LIMIT\s0 specified" 5
.IX Item "split on empty string with LIMIT specified"
.Vb 2
\&    $string = '';
\&    @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.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].