「use MooseX::AttributeHelpers」が抜けているとわけがわからないエラーになる件

相変わらず「モダンPerl入門」を読み進めている。 「2.3 Composite」まで進んだが、ここのサンプルソースが結構間違っていた。 カンマが抜けていたり、関数名が違っていたり。 そのあたりは簡単に予想できたのだけど、大変だったのは「Shape::Composite」に 「use MooseX::AttributeHelpers」がなかったために、わけのわからないエラーに悩まされたことだ。 気づいてみれば、この章の始めのほうにもMooseX::AttributeHelpersを多用するとは書いてあるのだが、以下のようなエラーからはとても思いつかなかった。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Could not load class (Moose::Meta::Attribute::Custom::Collection::Array) because : Can't locate Moose/Meta/Attribute/Custom/Collection/Array.pm in @INC (@INC contains: lib C:/strawberry/perl/lib C:/strawberry/perl/site/lib .) at C:/strawberry/perl/site/lib/Class/MOP.pm line 153.
Could not load class (Collection::Array) because : Can't locate Collection/Array.pm in @INC (@INC contains: lib C:/strawberry/perl/lib C:/strawberry/perl/site/lib .) at C:/strawberry/perl/site/lib/Class/MOP.pm line 153.
at C:/strawberry/perl/site/lib/Class/MOP.pm line 135
Class::MOP::load_first_existing_class('Moose::Meta::Attribute::Custom::Collection::Array', 'Collection::Array') called at C:/strawberry/perl/site/lib/Moose/Util.pm line 143
Moose::Util::resolve_metaclass_alias('Attribute', 'Collection::Array') called at C:/strawberry/perl/site/lib/Moose/Meta/Attribute.pm line 97
Moose::Meta::Attribute::interpolate_class('Moose::Meta::Attribute', 'provides', 'HASH(0x1160f6c)', 'metaclass', 'Collection::Array', 'isa', 'ArrayRef', 'default', 'CODE(0x1041e44)', ...) called at C:/strawberry/perl/site/lib/Moose/Meta/Attribute.pm line 86
Moose::Meta::Attribute::interpolate_class_and_new('Moose::Meta::Attribute', 'shapes', 'provides', 'HASH(0x1160f6c)', 'metaclass', 'Collection::Array', 'isa', 'ArrayRef', 'default', ...) called at C:/strawberry/perl/site/lib/Moose/Meta/Class.pm line 510
Moose::Meta::Class::_process_new_attribute('Moose::Meta::Class=HASH(0x104fb9c)', 'shapes', 'provides', 'HASH(0x1160f6c)', 'metaclass', 'Collection::Array', 'isa', 'ArrayRef', 'default', ...) called at C:/strawberry/perl/site/lib/Moose/Meta/Class.pm line 503
Moose::Meta::Class::_process_attribute('Moose::Meta::Class=HASH(0x104fb9c)', 'shapes', 'provides', 'HASH(0x1160f6c)', 'metaclass', 'Collection::Array', 'isa', 'ArrayRef', 'default', ...) called at C:/strawberry/perl/site/lib/Moose/Meta/Class.pm line 202
Moose::Meta::Class::add_attribute('Moose::Meta::Class=HASH(0x104fb9c)', 'shapes', 'provides', 'HASH(0x1160f6c)', 'metaclass', 'Collection::Array', 'isa', 'ArrayRef', 'default', ...) called at C:/strawberry/perl/site/lib/Moose.pm line 89
Moose::has('Shape::Composite', 'shapes', 'metaclass', 'Collection::Array', 'is', 'rw', 'isa', 'ArrayRef', 'auto_deref', ...) called at C:/strawberry/perl/site/lib/Moose/Exporter.pm line 201
Moose::Exporter::__ANON__('shapes', 'metaclass', 'Collection::Array', 'is', 'rw', 'isa', 'ArrayRef', 'auto_deref', 1, ...) called at lib/Shape/Composite.pm line 15
require Shape/Composite.pm called at p051.pl line 8
main::BEGIN() called at lib/Shape/Composite.pm line 0
eval {...} called at lib/Shape/Composite.pm line 0
Compilation failed in require at p051.pl line 8.
BEGIN failed--compilation aborted at p051.pl line 8.

最終的なShape::Compositeのソースは以下のとおり。

lib/Shape/Composite.pm

 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
package Shape::Composite;
use Moose;
use MooseX::AttributeHelpers;
with 'Shape';
has 'shapes' => (
metaclass => 'Collection::Array',
is  => 'rw',
isa => 'ArrayRef',
auto_deref => 1,
default => sub { [] },
provides => {
push => 'shape_add',
},
);
no Moose;
no MooseX::AttributeHelpers;
sub resize {
my ($self, $factor) = @_;
foreach my $shape ( $self->shapes ) {
$shape->resize( $factor );
}
}
sub print {
my $self = shift;
foreach my $shape ( $self->shapes ) {
$shape->print;
}
}
1;
comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。