"Konovalov, Vadim" <[EMAIL PROTECTED]> writes:

> Then following code has all desired behaviour within subs, as they seem to
> properly deleted at their time:
> 
> my %srefs; # srefs = "stringinfied references"
> 
> package better_sub;
> 
> sub DESTROY {
>   my $s = shift;
>   delete $srefs{"$s"};
>   print STDERR "I ($s) did my cleanup and go away\n";
> }
> 
> package main;
> 
> my $a = 1;
> 
> 
> for (0..3) {
>    $s = sub { $a };
>    bless $s, better_sub;
>    $t = $s if $_==2;
>    $srefs{"$s"}++;
> }
> #check if they are callable;
> 
> print STDERR '{{',$s->(),$s->(),$t->(),$t->(),"}}\n";
> 
> undef $s;
> undef $t;
> 
> print STDERR '[[',(join '++',sort keys %srefs),']]';
> 
> ----------------------------------------------------------------------
> 
> When I played with this (still simple) code, I came into conclusion that it
> does what required. (otherwise please let me know)

The %srefs hash does not do anything useful in the program above.  The
CVs called in the "print STDERR ...." line lives because the
references from $s and $t keep them alive.  If the CV was passed to
Tcl, $s and $t cleared, then the code would be gone before Tcl called
back.

The current Tcl.pm code works because it does '$srefs{"$s"} = $s'
instead of the '$srefs{"$s"}++' you do above.  This keeps the code
alive even after $s and $t are cleared.

--Gisle

Reply via email to