develooper Front page | perl.perl5.porters | Postings from January 2022

Re: Can't localize lexical variable $x at ...

Thread Previous | Thread Next
Dan Book
January 25, 2022 15:35
Re: Can't localize lexical variable $x at ...
Message ID:
On Tue, Jan 25, 2022 at 9:25 AM <> wrote:

> Why can't we? I think I might have known the answer once, but if I did
> I no longer recall it.
> I think of local() as "temporarily replace the value at this lvalue
> with a new value, restore it at the end of the current lexical scope".
> This is hugely useful in many situations that would otherwise require
> more and slower code with much more opportunity for error.
> Is there a technical reason why it would not be possible to implement?
> Or is it that we're worried it would be confusing for users? Or is it
> only that way back when (around 5.0, I guess) nobody thought it would
> be useful?

I might be making this up but my understanding is the technical reason is
that local replaces the variable, not the value; which works fine with a
package variable that is always enumerable, or an element of an aggregate,
but not with a lexical variable.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About