Perl FAQ 4.29: Why doesn't <tt>local($foo) = <FILE></tt> work right?

Perl FAQ 4.29

Why doesn't local($foo) = <FILE> work right?

Well, it does. The thing to remember is that local() provides an array context, and that the <FILE> syntax in an array context will read all the lines in a file. To work around this, use:

    local($foo);
    $foo = <FILE>;

You can use the scalar() operator to cast the expression into a scalar context:

    local($foo) = scalar(<FILE>);


Other resources at this site: