두 개의 Perl 모듈을 Moo으로 마이그레이션하기 시작했으나 setter/writer가 하나의 인수 만 가질 수 있기 때문에 멈추었습니다 (can not it?). 여기Perl Moo에서 setter/writter에 여러 인수를 전달하는 방법
package MyThing:
use Moo;
use Scalar::Util qw(blessed);
use SomeOtherThing;
has foo => (
is => 'rw',
coerce => sub {
return $_[0] if blessed($_[0]) and $_[0]->isa('SomeOtherThing');
return SomeOtherThing->new(@_); # does not work, because @_ == 1
},
);
을 간단한 사용 사례입니다 : 이것은 또한 강요에 적용
package MyApplication;
use MyThing;
$thing = MyThing->new;
$thing->foo('some', 'values'); # would like to have this shortcut
$thing->foo; # expected a SomeOtherThing
# must use this ugly form instead
$thing->foo(SomeOtherThing->new('some', 'values'));
여러 인수 설정을 지원 접근을 구현하는 쉬운 방법이 있나요?
해결 방법을 제공해 주셔서 감사합니다. 여전히 해결 방법이지만 내가 찾고있는 것이 아닙니다. 나는 그것을 할 수있는 방법을 발견하고 perl 패키지를 만들었다 : https://github.com/nichtich/MooX-AutoConstructor – Jakob