>On 5/18/07, Greg London <[EMAIL PROTECTED]> wrote:
>>
>> >sub replace_sub_for_instance {
>> >    my ($object, $subroutine_name, $new_subroutine) = @_;
>> >    no strict 'refs';
>> >    my $old_subroutine = \&$subroutine_name
>>  >        or die "Subroutine $subroutine_name not found";
>> >    my $object_name = refaddr($object);
>> >    *$subroutine_name = sub {
>> >        my $self = $_[0];
>> >        if (refaddr($self) eq $object_name) {
>> >            goto $new_subroutine;
>> >        }
>> >        else {
>> >            goto $old_subroutine;
>> >        }
>> >    };
>> >}
>> >
>> >Note that I was careful not to capture the object of interest in the
>> >subroutine because I didn't want to mess up a DESTROY.
>>
>> I'm just a tad confused about that last bit.
>> There may be advantages to using goto over recalling the method,
>> but I'm not sure how shifting the object off @_ will mess up a DESTROY call.
> 
>You're looking at the wrong part of the code.  I'm referring to how I
>made sure to capture refaddr before creating the anonymous sub so that
>the anonymous sub did not have $object in it anywhere.  That keeps
>$object from being in the closure, which means that $object won't be
>kept alive by a reference from the subroutine.


Ah, take my original code:

 

my $instance_to_skip = $mut;

my $intercept = sub {

   my $obj=shift(@_);

   if($obj eq $instance_to_skip) {

      return;

  } else {

     $obj->(@_);

  }

};

And change the first line to

 

my $instance_to_skip = $mut . '';

 

This forces the instance to be stringified,

and then you're storing the string in your

closure, not a reference.

 

 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to