Thanks, it’s good to know how to write it correctly.
The Type::Tiny::Manual::Coercions documentation specifically says what I’ve
written in my example; I’ve reproduced the text below.
If the code in the documentation could be updated to reflect a recommended,
working syntax, it’d probably save a lot of people like myself a lot of
debugging time.
Paul L.
——-
In our earlier example, we'd define the PathTiny type constraint as before:
class_type PathTiny, { class => "Path::Tiny" };
But then not define any coercions for it. Later, when using the type
constraint, we can add coercions:
my $ConfigFileType = PathTiny->plus_coercions(
Str, sub { "Path::Tiny"->new($_) },
Undef, sub { "Path::Tiny"->new("/etc/myapp/default.conf") },
);
has config_file => (
is => "ro",
isa => $ConfigFileType,
coerce => 1,
);
Where the PathTiny constraint is used in another part of the code, it will
not see these coercions, because they were added to the new anonymous type
constraint, not to the PathTiny constraint itself!
——-
On Fri, Jul 7, 2017 at 4:59 PM, twata_1@yahoo.co.jp via RT <
bug-Type-Tiny@rt.cpan.org> wrote:
Show quoted text> <URL:
https://rt.cpan.org/Ticket/Display.html?id=122305 >
>
> Hi,
>
> It seems to me that t2.pl works.
>
> Also, the following might be helpful:
>
> Type::Tiny and deep coercions
>
https://stackoverflow.com/questions/22981664/typetiny-and-deep-coercions
>
>
> I hope this helps.
>
> Thank you,
> --
> twata
>
>
> On 2017-7月-01 土 17:26:24, plambert@plambert.net wrote:
> > Given this code:
> >
> > #!/usr/bin/env perl
> >
> >
> > use Moose;
> >
> > use Type::Utils qw/class_type/;
> >
> > use Path::Tiny;
> >
> > use Types::Standard qw/Str/;
> >
> >
> > class_type PathTiny => { class => "Path::Tiny" };
> >
> >
> > has a_path => (
> >
> > is => 'ro',
> >
> > isa => PathTiny->plus_coercions(Str, sub { path shift @_ }),
> >
> > );
> >
> >
> >
> > __PACKAGE__->meta->make_immutable();
> >
> >
> > 1;
> >
> > I get this result:
> >
> > % perl t.pl
> >
> > Can’t locate object method “plus_coercions” via package "PathTiny"
> (perhaps
> > you forgot to load "PathTiny"?) at t.pl line 12.
> >
> > Why does this not work, and more importantly to me, how can I be use
> > plus_coercions with an object reference? Using
> > InstanceOf[‘Path::Tiny’]->plus_coercions(…) gives a similar result (it
> > returns an unblessed reference).
> >
> > Paul L.
>
>
>
>