in reply to Re^2: Trying to DESTROY() an object
in thread SOLVED: Trying to DESTROY() a (closure-wrapped) object
which outputssub mock { my $thing = shift; my $self; if (ref($thing) eq __PACKAGE__){ $self = $thing; } else { $self = bless {}, __PACKAGE__; } my $closure_self = $self; use Scalar::Util 'weaken'; weaken $closure_self; $self->{sub} = \&One::foo; *One::foo = sub { $closure_self->{x} = 'x'; return "baz\n"; }; return $self; }
as per the spec. Note that the $closure_self misdirect is necessary because of the Class method invocation:foo baz destroying... foo
which would yieldprint One::foo(); { #my $count = Count->new; my $bar = Count->mock; print One::foo(); } print One::foo();
Because $self immediately goes out of scope. It's still a weird misdirection since you are localizing a subroutine clobber and not changing an object behavior.foo Use of uninitialized value in subroutine dereference at script.pl line + 31. destroying... baz baz (in cleanup) Unable to create sub named "" at script.pl line 31.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Trying to DESTROY() an object
by Eily (Monsignor) on Dec 09, 2015 at 09:08 UTC | |
by kennethk (Abbot) on Dec 09, 2015 at 15:58 UTC | |
by Eily (Monsignor) on Dec 09, 2015 at 19:18 UTC | |
|
Re^4: Trying to DESTROY() an object
by stevieb (Canon) on Dec 08, 2015 at 19:01 UTC | |
by kennethk (Abbot) on Dec 08, 2015 at 20:31 UTC | |
by stevieb (Canon) on Dec 08, 2015 at 20:57 UTC |