in reply to Closures and scope of lexicals
The variable is captured (i.e. kept around), not the value.
I would not have expected this.
Weird. You read $i, but expect to get the value of something other than $i?
is this [...] something which can be found documented somewhere?
Closures are documented in perlsub. "If something more permanent is still aware of the lexical, it will stick around."
This means you can access the lexical beyond when it would normally be destroyed for going out of scope (which doesn't actually happen in your second snippet).
Reading the variable still gives its current value as usual.
is this a bug [...]?
Absolutely not.
Imagine if the following module didn't work because $i didn't get captured by get and set:
(These are true closures like your first snippet but unlike your second since $i goes out of scope at the end of the file before get and set are called.)package Foo; my $i; sub get { $i } sub set { $i = shift; } 1
is this [...] a quirk [...]?
Absolutely not.
Same in JavaScript.
const subrefs = []; let i = 0; for ( let j = 3; j--; ) { i++; subrefs.push( function(){ console.log( i ); } ); } for ( const subref of subrefs ) { subref(); }
3 3 3
Same in C#
List<Action> subrefs = new(); int i = 0; for (int j = 0; j < 3; ++j ) { i++; subrefs.Add( () => Console.WriteLine( i ) ); } foreach ( var subref in subrefs ) { subref(); }
3 3 3
Same in Python
subrefs = [] i = 0 for j in range(3): i = i + 1 def subref(): print( i ) subrefs.append( subref ) for subref in subrefs: subref()
3 3 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Closures and scope of lexicals
by haj (Vicar) on Oct 31, 2024 at 15:32 UTC | |
|
Re^2: Closures and scope of lexicals
by LanX (Saint) on Oct 31, 2024 at 14:07 UTC | |
by ikegami (Patriarch) on Oct 31, 2024 at 14:25 UTC | |
by LanX (Saint) on Oct 31, 2024 at 14:31 UTC | |
by ikegami (Patriarch) on Oct 31, 2024 at 14:35 UTC | |
by LanX (Saint) on Oct 31, 2024 at 14:37 UTC | |
| |
by ikegami (Patriarch) on Oct 31, 2024 at 14:38 UTC |