|
Perl4-to-Perl5 traps from having to do with parsing.
- * Parsing
-
Note the space between . and =
$string . = "more string";
print $string;
# perl4 prints: more string
# perl5 prints: syntax error at - line 1, near ". ="
|
|
- * Parsing
-
Better parsing in perl 5
sub foo {}
&foo
print("hello, world\n");
# perl4 prints: hello, world
# perl5 prints: syntax error
|
|
- * Parsing
-
"if it looks like a function, it is a function" rule.
print
($foo == 1) ? "is one\n" : "is zero\n";
# perl4 prints: is zero
# perl5 warns: "Useless use of a constant in void context" if using -w
|
|
- * Parsing
-
String interpolation of the $#array construct differs when braces are to
used around the name.
@a = (1..3);
print "${#a}";
# perl4 prints: 2
# perl5 fails with syntax error
@ = (1..3);
print "$#{a}";
# perl4 prints: {a}
# perl5 prints: 2
|
|
- * Parsing
-
When perl sees map { (or grep {), it has to guess whether the {
starts a BLOCK or a hash reference. If it guesses wrong, it will report a syntax error near
the } and the missing (or unexpected) comma.
Use unary + before { on a hash reference, and unary +
applied to the first thing in a BLOCK (after {), for perl to guess right all
the time. (See perlfunc/map.)
|
|