- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

8.3.2 Localization of special variables

If you localize a special variable, you'll be giving a new value to it, but its magic won't go away. That means that all side-effects related to this magic still work with the localized value.

This feature allows code like this to work :

# Read the whole contents of FILE in $slurp
{ local $/ = undef; $slurp = <FILE>; }

Note, however, that this restricts localization of some values ; for example, the following statement dies, as of perl 5.9.0, with an error Modification of a read-only value attempted, because the $1 variable is magical and read-only :

local $1 = 2;

Similarly, but in a way more difficult to spot, the following snippet will die in perl 5.9.0 :

sub f { local $_ = "foo"; print }
for ($1) {
    # now $_ is aliased to $1, thus is magic and readonly
    f();
}

See next section for an alternative to this situation.

WARNING: Localization of tied arrays and hashes does not currently work as described. This will be fixed in a future release of Perl; in the meantime, avoid code that relies on any particular behaviour of localising tied arrays or hashes (localising individual elements is still okay). See Localising Tied Arrays and Hashes Is Broken in perl58delta for more details.

ISBN 9781906966027Perl Language Reference ManualSee the print edition