Class::Accessor::Fastを触ってみる

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#utf8
use strict;
use warnings;
use utf8;
use Data::Dumper; sub p ($) { print Data::Dumper::Dumper(shift); }
package Point;
use base qw/Class::Accessor::Fast/;
__PACKAGE__->mk_accessors(qw/x y/);
package main;
my $p = Point->new;
p $p;
$p->x(128);
$p->y(256);
eval { $p->z(32); };
warn $@ if $@;
p $p->x;
p $p->y;
p $p;
1
2
3
4
5
6
7
8
Can't locate object method "z" via package "Point" at D:\devel\test\point.pl line 18.
$VAR1 = bless( {}, 'Point' );
$VAR1 = 128;
$VAR1 = 256;
$VAR1 = bless( {
'y' => 256,
'x' => 128
}, 'Point' );
 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
use utf8;
use Data::Dumper;
sub p ($) {
use warnings;
use strict 'refs';
print Data::Dumper::Dumper(shift @_);
}
package Point;
sub BEGIN {
use warnings;
use strict 'refs';
require base;
do {
'base'->import('Class::Accessor::Fast')
};
}
use warnings;
use strict 'refs';
'Point'->mk_accessors('x', 'y');
package main;
my $p = 'Point'->new;
p $p;
$p->x(128);
$p->y(256);
eval {
do {
$p->z(32)
}
};
warn $@ if $@;
p($p->x);
p($p->y);
p $p;
comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。