Skip Menu |

This queue is for tickets about the Type-Tiny CPAN distribution.

Report information
The Basics
Id: 122305
Status: resolved
Priority: 0/
Queue: Type-Tiny

People
Owner: Nobody in particular
Requestors: plambert [...] plambert.net
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Documented code using class_type with plus_coercions doesn’t work
Date: Sat, 1 Jul 2017 14:26:15 -0700
To: bug-Type-Tiny [...] rt.cpan.org
From: Paul Lambert <plambert [...] plambert.net>
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.
From: twata_1 [...] yahoo.co.jp
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: Show quoted text
> 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.
Subject: t2.pl
#!/usr/bin/env perl use Moose; use Type::Utils qw/class_type/; use Path::Tiny; use Types::Standard qw/Str/; my $PathTiny = class_type { class => "Path::Tiny" }; my $a_path = $PathTiny->plus_coercions(Str, sub { path shift @_ } ); has a_path => ( is => 'ro', isa => $a_path, coerce => 1, ); __PACKAGE__->meta->make_immutable(); 1;
Subject: Re: [rt.cpan.org #122305] Documented code using class_type with plus_coercions doesn’t work
Date: Fri, 7 Jul 2017 17:10:53 -0700
To: bug-Type-Tiny [...] rt.cpan.org
From: Paul Lambert <plambert [...] plambert.net>
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.
> > > >
You probably shouldn't be using Type::Utils inside a Moose class. It's really only meant for declaring types inside a type library. The problem in this case is that with this: PathType->plus_coercions( ... ) the "PathType" bit is treated by Perl as a string (i.e. class name). If you want it to act as a constant, like, say, "Str" is when you import it from Types::Standard, then you need to import "PathType" into your class at compile time — and you'd do that by declaring it in a type library and then importing the type library with "use".