icon Top 9 categories map      RocketAware > Perl >

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

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.


Search Perl pages


By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Integer, Floating point, Matrix, Statistics, Boolean, ...

Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...


Why doesn't "local($foo) = ;" work right?

local() gives list context to the right hand side of =. The < FH> read operation, like so many of Perl's functions and operators, can tell which context it was called in and behaves appropriately. In general, the scalar() function can help. This function does nothing to the data itself (contrary to popular myth) but rather tells its argument to behave in whatever its scalar fashion is. If that function doesn't have a defined scalar behavior, this of course doesn't help you (such as with sort()).

To enforce scalar context in this particular case, however, you need merely omit the parentheses:

    local($foo) = <FILE>;           # WRONG
    local($foo) = scalar(<FILE>);   # ok
    local $foo  = <FILE>;           # right

You should probably be using lexical variables anyway, although the issue is the same here:

    my($foo) = <FILE>;  # WRONG
    my $foo  = <FILE>;  # right

Source: Perl FAQ: Perl Language Issues
Copyright: Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
Next: How do I redefine a builtin function, operator, or method?

Previous: What's the difference between deep and shallow binding?

(Corrections, notes, and links courtesy of RocketAware.com)

[Overview Topics]

Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfaq7/Why_doesn_t_local_foo_FILE.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments