IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Closures and object life time in Perl
Here is my little piece of code

>>>>>>>>>>>>>>>>>>>>>>>>>>>>

{
package Class;

sub new
{
\treturn bless({});
}
sub DESTROY
{
\tprint "alas, I am undone\\n";
};
}


{
{
\tmy $x = Class->new ();
\tprint "declared\\n";
\t
\tsub IwishItWereIrrelevant
\t{
\t $x;
\t 1;
\t}
}
}

print "out of block\\n";


<<<<<<<<<<<<<<<<<<<<<<



Perl preserves access to functions beyod the function's scope, so $x survives to the end of program. Remove the function, and it will be deleted at the end of block.

And "my sub" is not there to help me. Oh bother!

------

179. I will not outsource core functions.
--
[link|http://omega.med.yale.edu/~pcy5/misc/overlord2.htm|.]

New Why is this an issue for you?
Perl is reference counted. At block end, variables that have no references left are cleaned up. So if you keep a reference to a variable in a closure, it survives longer than if you don't.

With the exception of not cleaning up circular data structures, this does what most people want, when they want it done.

I'm not sure what need you have that isn't being met. If you want a subroutine which is lexically scoped, though, you can easily do it with the following trick:
\n  my $lexically_scoped_sub = sub {print "Body here\\n"};\n  # time passes.\n  $lexically_scoped_sub->(@arguments, @here);\n

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
     Closures and object life time in Perl - (Arkadiy) - (1)
         Why is this an issue for you? - (ben_tilly)

Brain bleach now available in the 50 gallon jug.
60 ms