Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moose - coercing from Num to ArrayRef[Num]?

Tags:

perl

moose

Ok, what am I doing wrong - Moose is ignoring my coercion:

package moo;

use Moose;
use Moose::Util::TypeConstraints;

subtype Bar => as 'ArrayRef[Num]';

coerce 'Bar' =>
  from 'Num' => via { [ 10 ] }; # this doesn't seem to be getting called

has x => (
  is => 'rw',
  isa => 'Bar',
);

package main;

my $m1 = moo->new(x => [ 3 ]);  # works
my $m2 = moo->new(x => 5);      # doesn't work
like image 848
ErikR Avatar asked Feb 28 '26 12:02

ErikR


1 Answers

Maybe you forgot coerce => 1 while defining x attribute.

has x => ( is  => 'rw', isa => 'Bar', coerce => 1 );

`

like image 119
Marco De Lellis Avatar answered Mar 03 '26 05:03

Marco De Lellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!