Website hosting service by Active-Venture.com
  

 Back to Index

Context Traps - scalar, list contexts

* (list context)

The elements of argument lists for formats are now evaluated in list context. This means you can interpolate list values now.

 
    @fmt = ("foo","bar","baz");
    format STDOUT=
    @<<<<< @||||| @>>>>>
    @fmt;
    .
    write;

    # perl4 errors:  Please use commas to separate fields in file
    # perl5 prints: foo     bar      baz  

* (scalar context)

The caller() 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.

 
    caller() ? (print "You rang?\n") : (print "Got a 0\n");

    # perl4 errors: There is no caller
    # perl5 prints: Got a 0  

* (scalar context)

The comma operator in a scalar context is now guaranteed to give a scalar context to its arguments.

 
    @y= ('a','b','c');
    $x = (1, 2, @y);
    print "x = $x\n";

    # Perl4 prints:  x = c   # Thinks list context interpolates list
    # Perl5 prints:  x = 3   # Knows scalar uses length of list  

* (list, builtin)

sprintf() 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:

 
    @z = ('%s%s', 'foo', 'bar');
    $x = sprintf(@z);
    print $x;

    # perl4 prints: foobar
    # perl5 prints: 3  

printf() works the same as it did in Perl 4, though:

 
    @z = ('%s%s', 'foo', 'bar');
    printf STDOUT (@z);

    # perl4 prints: foobar
    # perl5 prints: foobar  

 

 

 

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

 

[ Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.   ]

 

 
 
 

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