[comp.lang.perl] local

leo@aai.com (07/21/90)

This one bit me tonight:

While (<FILE>) {
    do foo();
    .
    .
}

sub foo {
    local($line) = <FILE>;
    .
    .
}

didn't work as expected, but

sub foo {
    local($line);
    $line = <FILE>;
}

did what I wanted.

Anyone care to explain why?  Is this a documented restriction?
-- 
Leo	leo@aai.com   leo%aai@uunet.uu.net   ...uunet!aai!leo
-- 
Leo	leo@aai.com   leo%aai@uunet.uu.net   ...uunet!aai!leo

flee@guardian.cs.psu.edu (Felix Lee) (07/22/90)

>    local($line) = <FILE>;
>didn't work as expected, but
>    local($line);
>    $line = <FILE>;

local($line) is an array, so the <FILE> gets evaluated in an array
context, which sucks in the entire file as an array of lines.  The
first line gets put in $line, and the rest of the lines disappear.

The distinction between array and scalar contexts can be nasty.
--
Felix Lee	flee@cs.psu.edu