私はいくつかのPerlモジュールをMooに移行し始めましたが、セッター/ライターが1つの引数しか持てないために行き詰まりました(そうではありませんか?)。これは強制にも当てはまります。
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') );
複数の引数を使用した設定をサポートするアクセサーを実装する簡単な方法はありますか?