On Mon, Sep 11, 2023 at 12:36:41PM +0200, Ruud H.G. van Tol via perl5-porters wrote: > > On 2023-09-08 16:27, Dave Mitchell wrote: > > Internally, perl is mainly based around Scalar Value (SV) structures, > > which hold a single value. Entities such as $x or $a[0] are all SVs. These > > SVs include a reference count (RC) field which, when it reaches zero, > > triggers the freeing of the SV. For the basics, consider the following > > code: > > > > sub f { > > my $x = ...; > > my $y = ...; > > return \$y; > > } > > my $ref = f(); > > > > On return from the function, the SV associated with $x is freed in a > > timely manner. This is because its RC starts as 1, and is reduced to 0 on > > scope exit. Conversely, the SV bound to $y has its RC increased to 2 by a > > reference being taken to it, then back down to 1 when $y goes out of > > scope. So it lives on, accessible as $$ref. This is all good. > > <mode=pedantic> > > Unless something like: > > my $y= sub { $x }; It's not clear to me what point you're making here. My example was supposed to show that lexical variables are normally freed when going out of scope unless something extends their lifetime. An example of the latter being taking a reference to it. Including it in a closure would be another example of extending its lifetime. -- The crew of the Enterprise encounter an alien life form which is surprisingly neither humanoid nor made from pure energy. -- Things That Never Happen in "Star Trek" #22Thread Previous | Thread Next