Website hosting service by Active-Venture.com
  

 Back to Index

Interpolation Traps

Perl4-to-Perl5 traps having to do with how things get interpolated within certain expressions, statements, contexts, or whatever.

* Interpolation

@ now always interpolates an array in double-quotish strings.

 
    print "To: someone@somewhere.com\n";

    # perl4 prints: To:someone@somewhere.com
    # perl < 5.6.1, error : In string, @somewhere now must be written as \@somewhere
    # perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string  

* Interpolation

Double-quoted strings may no longer end with an unescaped $ or @.

 
    $foo = "foo$";
    $bar = "bar@";
    print "foo is $foo, bar is $bar\n";

    # perl4 prints: foo is foo$, bar is bar@
    # perl5 errors: Final $ should be \$ or $name  

Note: perl5 DOES NOT error on the terminating @ in $bar

* Interpolation

Perl now sometimes evaluates arbitrary expressions inside braces that occur within double quotes (usually when the opening brace is preceded by $ or @).

 
    @www = "buz";
    $foo = "foo";
    $bar = "bar";
    sub foo { return "bar" };
    print "|@{w.w.w}|${main'foo}|";

    # perl4 prints: |@{w.w.w}|foo|
    # perl5 prints: |buz|bar|  

Note that you can use strict; to ward off such trappiness under perl5.

* Interpolation

The construct "this is $$x" used to interpolate the pid at that point, but now tries to dereference $x. $$ by itself still works fine, however.

 
    $s = "a reference";
    $x = *s;
    print "this is $$x\n";

    # perl4 prints: this is XXXx   (XXX is the current pid)
    # perl5 prints: this is a reference  

* Interpolation

Creation of hashes on the fly with eval "EXPR" now requires either both $'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 eval{} if possible.

 
    $hashname = "foobar";
    $key = "baz";
    $value = 1234;
    eval "\$$hashname{'$key'} = q|$value|";
    (defined($foobar{'baz'})) ?  (print "Yup") : (print "Nope");

    # perl4 prints: Yup
    # perl5 prints: Nope  

Changing

 
    eval "\$$hashname{'$key'} = q|$value|";  

to

 
    eval "\$\$hashname{'$key'} = q|$value|";  

causes the following result:

 
    # perl4 prints: Nope
    # perl5 prints: Yup  

or, changing to

 
    eval "\$$hashname\{'$key'\} = q|$value|";  

causes the following result:

 
    # perl4 prints: Yup
    # perl5 prints: Yup
    # and is compatible for both versions
  

* Interpolation

perl4 programs which unconsciously rely on the bugs in earlier perl versions.

 
    perl -e '$bar=q/not/; print "This is $foo{$bar} perl5"'

    # perl4 prints: This is not perl5
    # perl5 prints: This is perl5  

* Interpolation

You also have to be careful about array references.

 
    print "$foo{"

    perl 4 prints: {
    perl 5 prints: syntax error  

* Interpolation

Similarly, watch out for:

 
    $foo = "baz";
    print "\$$foo{bar}\n";

    # perl4 prints: $baz{bar}
    # perl5 prints: $  

Perl 5 is looking for $foo{bar} which doesn't exist, but perl 4 is happy just to expand $foo to "baz" by itself. Watch out for this especially in eval's.

* Interpolation

qq() string passed to eval

 
    eval qq(
        foreach \$y (keys %\$x\) {
            \$count++;
        }
    );

    # perl4 runs this ok
    # perl5 prints: Can't find string terminator ")"  

 

 

 

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

 

[ Large increases in cost with questionable increases in performance can be tolerated only in race horses and women.   ]

 

 
 
 

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