1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#!/usr/bin/perl
use v5.18;
use warnings;
use Test2::V0;
use Object::Pad 0.800;
{
class Counter {
field $count;
my $allcount = 0;
method inc { $count++; $allcount++ }
method count { $count }
sub allcount { $allcount }
}
my $countA = Counter->new;
my $countB = Counter->new;
$countA->inc;
$countB->inc;
is( $countA->count, 1, '$countA->count' );
is( Counter->allcount, 2, 'Counter->allcount' );
}
# anon methods can capture lexicals (RT132178)
{
class Generated {
foreach my $letter (qw( x y z )) {
my $code = method {
return uc $letter;
};
no strict 'refs';
*$letter = $code;
}
}
my $g = Generated->new;
is( $g->x, "X", 'generated anon method' );
is( $g->y, "Y", 'generated anon method' );
is( $g->z, "Z", 'generated anon method' );
}
done_testing;
|