Website hosting service by Active-Venture.com
  

 Back to Index

Parsing Traps

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.)

 

 

 

Domain name registration service & domain search - 
Register cheap domain name from $7.95 and enjoy free domain services 
 

Cheap domain name search service -
Domain name services at just
$8.95/year only
 

Register domain name -
Buy domain name registration and cheap domain transfer at low, affordable price.

© 2002-2004 Active-Venture.com Web Site Hosting Service

 

[ Software is like entropy. It is difficult to grasp, weighs nothing, and obeys the second law of thermodynamics; i.e., it always increases.   ]

 

 
 
 

Disclaimer: This documentation is provided only for the benefits of our web hosting customers.
For authoritative source of the documentation, please refer to http://www.perldoc.com