More Related Content More from Masahiro Honma (20) 循環参照のはなし4. my ($ref1, $ref2, $ref3);
$ref1 = $ref2;
$ref2 = $ref3;
$ref3 = $ref1;
$ref1 $ref2 $ref3
15. (1)
Template::Plugin::Filter
(TT-2.2)
Template::Plugin
return $self->{ _STATIC_FILTER } ||= sub {
$self->filter(shift);
};
16. (2)
AnyEvent
my $t; $t = AE::timer 1, 0, sub {
...;
undef $t;
};
$t sub { ... }
17. (2)
AnyEvent
my $t; $t = AE::timer 1, 0, sub {
...;
undef $t;
};
$t sub { ... }
18. (2)
AnyEvent
my $t; $t = AE::timer 1, 0, sub {
...;
undef $t;
};
$t
19. (2)
AnyEvent
my $t; $t = AE::timer 1, 0, sub {
...;
undef $t;
};
$t
20. (2)
AnyEvent
my $t; $t = AE::timer 1, 0, sub {
...;
undef $t;
};
22. (3)
use Devel::Peek; Dump($f);
SV = IV(0x100827fd8) at 0x100827fe0
REFCNT = 2
RV = 0x100827878
SV = PVCV(0x10082b5f8) at 0x100827878
REFCNT = 1
GVGV::GV = 0x100856bc0 "main" :: "__ANON__"
OUTSIDE = 0x1008032a0 (ANON)
SV = PVCV(0x100852a90) at 0x1008032a0
REFCNT = 1
GVGV::GV = 0x100856bc0 "main" :: "__ANON__"
PADNAME = 0x1008277e8(0x100287000) PAD = 0x100827800(0x10025c0e0)
1. 0x100827fe0<2> FAKE "$f" flags=0x0 index=1
24. 1) ps
% perl
for (1 .. 10000) {
my $ref; $ref = $ref;
warn `ps -o rss= -p $$` if $_ % 1000 == 0;
}
1272
1304
1328
1352
1376
...
26. 2) Test::LeakTrace
use Test::LeakTrace;
no_leaks_ok { my $ref; $ref = $ref };
not ok 1 - leaks 1 <= 0
# Failed test 'leaks 1 <= 0'
# at - line 4.
# '1'
# <=
# '0'
# leaked REF(0x10083b610) from - line 4.
# SV = IV(0x10083b608) at 0x10083b610
# REFCNT = 1
# FLAGS = (PADMY,ROK)
# RV = 0x10083b610
# SV = IV(0x10083b608) at 0x10083b610
# REFCNT = 1
# FLAGS = (PADMY,ROK)
# RV = 0x10083b610
# SV = IV(0x10083b608) at 0x10083b610
# REFCNT = 1
# FLAGS = (PADMY,ROK)
# RV = 0x10083b610
# SV = IV(0x10083b608) at 0x10083b610
...
27. 3) Devel::Cycle
use Devel::Cycle;
my ($ref1, $ref2, $ref3);
$ref1 = $ref2;
$ref2 = sub { $ref3 };
$ref3 = [$ref1];
find_cycle($ref1);
Cycle (1):
$$A => &B
$B variable $ref3 => $C
$$C => @D
$D->[0] => $A
29. 5) DESTROY
sub Ref1::DESTROY { warn "destroyed REF1" }
sub Ref2::DESTROY { warn "destroyed REF2" }
my $x;
my $f = bless sub {
$x;
bless sub { $x } => "Ref2";
} => "Ref1";
$f->();
destroyed REF2 at - line 2.
destroyed REF1 at - line 1.
33. my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
};
$self->{handler}->();
sub { ... }
$self “Hokkaido.pm”
34. 1)
my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
undef $self;
};
$self->{handler}->();
sub { ... }
$self “Hokkaido.pm”
35. 1)
my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
undef $self;
};
$self->{handler}->();
sub { ... }
$self “Hokkaido.pm”
36. 1)
my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
undef $self;
};
$self->{handler}->();
sub { ... }
“Hokkaido.pm”
37. 1)
my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
undef $self;
};
$self->{handler}->();
sub { ... }
“Hokkaido.pm”
38. 1)
my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
undef $self;
};
$self->{handler}->();
39. 1)
( )
($self->{handler} )
40. 2) Scalar::Util::weaken
my $self = {name => "Hokkaido.pm", handler => undef};
Scalar::Util::weaken(my $weaken_self = $self);
$self->{handler} = sub {
print "Hello, ", $weaken_self->{name}, ".n";
};
$self->{handler}->();
sub { ... }
$self “Hokkaido.pm”
41. 2) Scalar::Util::weaken
my $self = {name => "Hokkaido.pm", handler => undef};
Scalar::Util::weaken($self); #
$self->{handler} = sub {
print "Hello, ", $self->{name}, ".n";
};
$self->{handler}->();
42. 3)
my $self = {name => "Hokkaido.pm", handler => undef};
my $name = $self->{name};
$self->{handler} = sub {
print "Hello, ", $name, ".n";
};
$self->{handler}->();
sub { ... }
$self “Hokkaido.pm”
43. 4)
my $self = {name => "Hokkaido.pm", handler => undef};
$self->{handler} = sub {
my $self = shift;
print "Hello, ", $self->{name}, ".n";
};
$self->{handler}->($self);
sub { ... }
$self “Hokkaido.pm”
45. 5) Data::Decycle
use Data::Decycle;
my $guard = Data::Decycle->new(
my $self = {name => "Hokkaido.pm", handler => undef}
);
$self->{handler} = sub {
"Hello, ", $self->{name}, ".n";
};
$self->{handler}->();
49. :Perl 5.8.8
my $x = bless {}, "Dummy";
(sub { $x; sub { $x } })->();
my $x = bless {}, "Dummy";
(sub { my $x = $x; sub { warn $x } })->();
51. :Obj-C ARC
retain release Perl
CBRFuture *future = [[CBRFuture alloc] init];
__weak CBRFuture *weakenFuture = future;
leftHandle(self, ^(id e) {
current = nil;
leftSend(weakenFuture, e);
});
52. :Obj-C Guard
NSString *local = @"OK";
static void changeLocalValue(BOOL willBreak) {
id original = local;
CBRGuard *guard = [[CBRGuard alloc] initGuard:^{
local = original;
}];
local = @"LOCALVALUE1";
}
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n &#x3010;5&#x5206;&#x3011;\n &#x3010;5&#x5206;&#x3011;\n &#x3010;5&#x5206;&#x3011;\n &#x3010;5&#x5206;&#x3011;\n \n \n \n &#x3088;&#x3044;: &#x30EA;&#x30FC;&#x30AF;&#x3057;&#x3066;&#x308B;&#x304B;&#x306E;&#x5224;&#x5B9A;&#x3068;&#x3057;&#x3066;&#x306F;&#x6027;&#x683C;&#x3001;&#x308F;&#x308B;&#x3044;: &#x539F;&#x59CB;&#x7684;&#x3001;&#x74B0;&#x5883;&#x4F9D;&#x5B58;\n &#x3088;&#x3044;: &#x30EA;&#x30FC;&#x30AF;&#x5168;&#x822C;&#x3092;&#x898B;&#x3064;&#x3051;&#x3089;&#x308C;&#x308B;&#x3001;&#x308F;&#x308B;&#x3044;: &#x7D50;&#x679C;&#x306E;&#x8868;&#x793A;&#x304C;&#x3054;&#x3064;&#x3044;\n &#x3088;&#x3044;: &#x7D50;&#x679C;&#x304C;&#x307F;&#x3084;&#x3059;&#x3044;&#x3001;&#x3060;&#x3081;: &#x5FAA;&#x74B0;&#x53C2;&#x7167;&#x3092;&#x63A2;&#x3059;&#x3060;&#x3051;&#x3002;XS&#x306E;&#x30EA;&#x30FC;&#x30AF;&#x306F;&#x691C;&#x51FA;&#x3067;&#x304D;&#x306A;&#x3044;&#x3002;OUTER&#x7D61;&#x307F;&#x3082;&#x7121;&#x7406;&#x307D;&#x3002;\n\n &#x3088;&#x3044;: &#x7C21;&#x5358;&#x306B;&#x30EA;&#x30FC;&#x30AF;&#x691C;&#x51FA;&#x3001; &#x308F;&#x308B;&#x3044;: &#x30AA;&#x30D6;&#x30B8;&#x30A7;&#x30AF;&#x30C8;&#x9650;&#x5B9A;\n &#x3010;10&#x5206;&#x3011;&#x3088;&#x3044;: &#x89E3;&#x653E;&#x30BF;&#x30A4;&#x30DF;&#x30F3;&#x30B0;&#x304C;&#x6B63;&#x78BA;&#x306B;&#x308F;&#x304B;&#x308B;(&#x4E2D;&#x306E;&#x30AF;&#x30ED;&#x30FC;&#x30B8;&#x30E3;&#x304B;&#x3089;&#x89E3;&#x653E;&#x3055;&#x308C;&#x308B;) &#x308F;&#x308B;&#x3044;: &#x3072;&#x3069;&#x3044;&#x30CF;&#x30C3;&#x30AF;\n \n \n \n \n \n \n \n \n \n \n \n \n &#x3010;15&#x5206;&#x3011;undef &#x30A8;&#x30E9;&#x30FC;&#x306E;&#x539F;&#x56E0;&#x3068;&#x306A;&#x308B;&#x3002;&#x3053;&#x306E;&#x4F8B;&#x3060;&#x3068;&#x30EA;&#x30FC;&#x30AF;&#x3059;&#x308B;&#x3002;\n &#x3053;&#x306E;&#x4F8B;&#x3060;&#x3068;&#x3001;weaken&#x3088;&#x308A;&#x3053;&#x3061;&#x3089;&#x304C;&#x81EA;&#x7136;\n API&#x3092;&#x5909;&#x66F4;&#x3057;&#x3001;&#x30AF;&#x30ED;&#x30FC;&#x30B8;&#x30E3;&#x3092;&#x6E21;&#x3055;&#x305A;&#x306B;&#x3059;&#x3080;&#x3088;&#x3046;&#x306B;&#x3059;&#x308B;\n \n \n \n \n \n \n \n \n &#x7532;&#x6590;&#x4E92;&#x63DB;&#x306E;&#x305F;&#x3081;&#x306B;autorelease &#x3067;&#x8FD4;&#x3057;&#x3066;&#x304F;&#x308B;&#x3053;&#x3068;&#x304C;&#x3042;&#x308B;&#x304C;&#x3001;&#x305D;&#x308C;&#x3060;&#x3068;&#x3046;&#x307E;&#x304F;&#x3044;&#x304B;&#x306A;&#x3044;&#x3002;&#x4ED5;&#x69D8;&#x3092;&#x3088;&#x304F;&#x8ABF;&#x3079;&#x308B;&#x5FC5;&#x8981;&#x3042;&#x308A;&#x3002;\n \n