X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperltrap.pod;h=0ad0086a599a4656411bb92932a706b1cb099045;hb=9b5c3821be1f2a9a84772171c8bbadbf9cfc4a53;hp=262937910db355249a0d107b8762c7092b772999;hpb=d1be9408a3c14848d30728674452e191ba5fffaa;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perltrap.pod b/pod/perltrap.pod index 2629379..0ad0086 100644 --- a/pod/perltrap.pod +++ b/pod/perltrap.pod @@ -18,6 +18,11 @@ Accustomed B users should take special note of the following: =item * +A Perl program executes only once, not once for each input line. You can +do an implicit loop with C<-n> or C<-p>. + +=item * + The English module, loaded via use English; @@ -143,9 +148,9 @@ gives you. =back -=head2 C Traps +=head2 C/C++ Traps -Cerebral C programmers should take note of the following: +Cerebral C and C++ programmers should take note of the following: =over 4 @@ -159,13 +164,14 @@ You must use C rather than C. =item * -The C and C keywords from C become in -Perl C and C, respectively. -Unlike in C, these do I work within a C construct. +The C and C keywords from C become in Perl C +and C, respectively. Unlike in C, these do I work within a +C construct. See L. =item * -There's no switch statement. (But it's easy to build one on the fly.) +There's no switch statement. (But it's easy to build one on the fly, +see L) =item * @@ -173,7 +179,9 @@ Variables begin with "$", "@" or "%" in Perl. =item * -Comments begin with "#", not "/*". +Comments begin with "#", not "/*" or "//". Perl may interpret C/C++ +comments as division operators, unterminated regular expressions or +the defined-or operator. =item * @@ -205,6 +213,11 @@ Seasoned B programmers should take note of the following: =item * +A Perl program executes only once, not once for each input line. You can +do an implicit loop with C<-n> or C<-p>. + +=item * + Backreferences in substitutions use "$" rather than "\". =item * @@ -254,6 +267,13 @@ The arguments are available via @ARGV, not $1, $2, etc. The environment is not automatically made available as separate scalar variables. +=item * + +The shell's C uses "=", "!=", "<" etc for string comparisons and "-eq", +"-ne", "-lt" etc for numeric comparisons. This is the reverse of Perl, which +uses C, C, C for string comparisons, and C<==>, C C<< < >> etc +for numeric comparisons. + =back =head2 Perl Traps @@ -651,6 +671,16 @@ are to used around the name. # perl4 prints: {a} # perl5 prints: 2 +=item * Parsing + +When perl sees C (or C), it has to guess whether the C<{> +starts a BLOCK or a hash reference. If it guesses wrong, it will report +a syntax error near the C<}> and the missing (or unexpected) comma. + +Use unary C<+> before C<{> on a hash reference, and unary C<+> applied +to the first thing in a BLOCK (after C<{>), for perl to guess right all +the time. (See L.) + =back =head2 Numerical Traps @@ -662,18 +692,24 @@ operands, or output from same. =item * Numerical -Formatted output and significant digits +Formatted output and significant digits. In general, Perl 5 +tries to be more precise. For example, on a Solaris Sparc: print 7.373504 - 0, "\n"; printf "%20.18f\n", 7.373504 - 0; # Perl4 prints: - 7.375039999999996141 - 7.37503999999999614 + 7.3750399999999996141 + 7.375039999999999614 # Perl5 prints: 7.373504 - 7.37503999999999614 + 7.375039999999999614 + +Notice how the first result looks better in Perl 5. + +Your results may vary, since your floating point formatting routines +and even floating point format may be slightly different. =item * Numerical @@ -1207,6 +1243,10 @@ repeatedly, like C or C. # perl4 prints: perl4 # perl5 prints: perl5 +=item * Regular Expression + +Unlike in Ruby, failed matches in Perl do not reset the match variables +($1, $2, ..., C<$`>, ...). =back @@ -1328,13 +1368,12 @@ within certain expressions, statements, contexts, or whatever. =item * Interpolation -Double-quoted strings may no longer end with an unescaped $ or @. +Double-quoted strings may no longer end with an unescaped $. $foo = "foo$"; - $bar = "bar@"; - print "foo is $foo, bar is $bar\n"; + print "foo is $foo\n"; - # perl4 prints: foo is foo$, bar is bar@ + # perl4 prints: foo is foo$ # perl5 errors: Final $ should be \$ or $name Note: perl5 DOES NOT error on the terminating @ in $bar @@ -1420,13 +1459,26 @@ perl4 programs which unconsciously rely on the bugs in earlier perl versions. =item * Interpolation -You also have to be careful about array references. +You also have to be careful about array and hash brackets during +interpolation. + + print "$foo[" + + perl 4 prints: [ + perl 5 prints: syntax error print "$foo{" perl 4 prints: { perl 5 prints: syntax error +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. + + print "$foo\["; + print "$foo\{"; + =item * Interpolation Similarly, watch out for: