in reply to Moose roles with extends
G'day jjyoung,
Welcome to the monastery.
The short answer may simply be what the error message is telling you. An instance of GenericCommandHandler IS-A GenericCommandHandler and IS-A CoRHandler (both of which are classes), but it IS-NOT-A ChainOfResponsibilityIf which is a role.
If that doesn't answer your question, then a longer answer may require some further input from you.
I created a './PM/1081903/ directory and populated it with:
$ cat ChainOfResponsibilityIf.pm package PM::1081903::ChainOfResponsibilityIf; use Moose::Role; requires 'handleRequest'; requires 'addHandler'; 1;
$ cat CoRHandler.pm package PM::1081903::CoRHandler; use Moose; use Carp qw(confess croak); with 'PM::1081903::ChainOfResponsibilityIf'; sub handleRequest { 1 } sub addHandler { 1 } sub _processCommand { 1 } 1;
$ cat GenericCommandHandler.pm package PM::1081903::GenericCommandHandler; use Moose; extends 'PM::1081903::CoRHandler'; override '_processCommand' => sub { }; 1;
$ cat GCHSuccessor.pm package PM::1081903::GCHSuccessor; use Moose; has 'successor' => ( is => 'rw', isa => 'PM::1081903::ChainOfResposibilityIf', ); 1;
I then ran this script:
#!/usr/bin/env perl use strict; use warnings; use PM::1081903::GenericCommandHandler; use PM::1081903::GCHSuccessor; my $gchs = PM::1081903::GCHSuccessor::->new( successor => PM::1081903::GenericCommandHandler::->new() );
I got a lot of error output which started the same as you reported "Attribute (successor) does not pass the type constraint ...". See the spoiler for the full message.
Attribute (successor) does not pass the type constraint because: Valid +ation failed for 'PM::1081903::ChainOfResposibilityIf' with value PM: +:1081903::GenericCommandHandler=HASH(0x7fa22882a610) (not isa PM::108 +1903::ChainOfResposibilityIf) at /Users/ken/perl5/perlbrew/perls/perl +-5.18.1t/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moose/Meta/A +ttribute.pm line 1279. Moose::Meta::Attribute::verify_against_type_constraint('Moose::Met +a::Attribute=HASH(0x7fa2292b89e0)', 'PM::1081903::GenericCommandHandl +er=HASH(0x7fa22882a610)', 'instance', 'PM::1081903::GCHSuccessor=HASH +(0x7fa2288525c8)') called at /Users/ken/perl5/perlbrew/perls/perl-5.1 +8.1t/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moose/Meta/Attri +bute.pm line 1266 Moose::Meta::Attribute::_coerce_and_verify('Moose::Meta::Attribute +=HASH(0x7fa2292b89e0)', 'PM::1081903::GenericCommandHandler=HASH(0x7f +a22882a610)', 'PM::1081903::GCHSuccessor=HASH(0x7fa2288525c8)') calle +d at /Users/ken/perl5/perlbrew/perls/perl-5.18.1t/lib/site_perl/5.18. +1/darwin-thread-multi-2level/Moose/Meta/Attribute.pm line 536 Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Att +ribute=HASH(0x7fa2292b89e0)', 'Moose::Meta::Instance=HASH(0x7fa228843 +e80)', 'PM::1081903::GCHSuccessor=HASH(0x7fa2288525c8)', 'HASH(0x7fa2 +290d5108)') called at /Users/ken/perl5/perlbrew/perls/perl-5.18.1t/li +b/site_perl/5.18.1/darwin-thread-multi-2level/Class/MOP/Class.pm line + 525 Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x +7fa22929a2e8)', 'HASH(0x7fa2290d5108)') called at /Users/ken/perl5/pe +rlbrew/perls/perl-5.18.1t/lib/site_perl/5.18.1/darwin-thread-multi-2l +evel/Class/MOP/Class.pm line 498 Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x7fa22929a +2e8)', 'HASH(0x7fa2290d5108)') called at /Users/ken/perl5/perlbrew/pe +rls/perl-5.18.1t/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moos +e/Meta/Class.pm line 284 Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x7fa22929 +a2e8)', 'HASH(0x7fa2290d5108)') called at /Users/ken/perl5/perlbrew/p +erls/perl-5.18.1t/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moo +se/Object.pm line 28 Moose::Object::new('PM::1081903::GCHSuccessor', 'successor', 'PM:: +1081903::GenericCommandHandler=HASH(0x7fa22882a610)') called at ./pm_ +example.pl line 9
I then changed this line in GCHSuccessor.pm
isa => 'PM::1081903::ChainOfResposibilityIf',
to
isa => 'PM::1081903::CoRHandler',
I reran the same script and no output at all was generated. Perhaps this is the (type of) change you need.
I then added this line to the end of that script:
print $gchs->successor->handleRequest(), "\n";
This produced 1 as its only output. So, the value of the successor attribute of the PM::1081903::GCHSuccessor object DOES the PM::1081903::ChainOfResposibilityIf role. Is that what you were trying to test?
If some, or all, of that hasn't answered your question, or my guesses at your class stucture were wrong, please provide actual code that we can run, along with a clear description of what trying to test or learn, such that we can point you in the right direction.
-- Ken
|
|---|