Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

MooseX::Types::TypeDecorator error

by KSURi (Monk)
on Jul 16, 2009 at 13:02 UTC ( [id://780700]=perlquestion: print w/replies, xml ) Need Help??

KSURi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks!

I'm writing a tool using a bunch of modern perl modules (MooseX::Declare, MooseX::Types, MooseX::MultiMethods). But it doesn't work:

Subroutine My::Tool::method redefined at /usr/local/share/perl/5.10.0/ +MooseX/Method/Signatures.pm line 38. Argument cannot be 'type' at /usr/local/share/perl/5.10.0/MooseX/Types +/TypeDecorator.pm line 81 MooseX::Types::TypeDecorator::new('MooseX::Types::TypeDecorator=HA +SH(0xa3f6fe8)', 'type', 'mass', 'from', 'kg', 'to', 'g') called at /h +ome/surikov/lib/My/Tool.pm line 12 My::Tool::__ANON__('My::Tool=HASH(0x93cf9e0)', 'HASH(0x93cf760)') +called at t.pl line 56
I'm definitely missing something, just don't understand what. Any suggestions?

PS: debugging with MooseX::Declare becomes quite difficult and takes a lot of time =(

I have defined my own data types in a separate package, like this:
package My::Types; use strict; use warnings; use MooseX::Types -declare => [qw(Measure MeasureType MeasureUnit some + other types)]; use MooseX::Types::Moose qw(Num Int Str); use MooseX::Types::Structured qw(Dict); use List::MoreUtils qw(any); my @measure_types = qw(some data here); my @measure_units = qw(and here); subtype MeasureType, as Str, where { my $measure_type = $_; any { $measure_type eq lc } @meas +ure_types }, message { 'Unknown measure type' }; subtype MeasureUnit, as Str, where { my $measure_unit = $_; any { $measure_unit eq lc } @meas +ure_units }, message { 'Unknown measure unit' }; subtype Measure, as Dict[ type => MeasureType, from => MeasureUnit, to => MeasureUnit, amount => Num, ], message { 'Invalid Measure struct' }; # more subtypes goes here
And here is the usage:
class My::Tool { # ... multi method convert(Measure $measure) { my $converter = My::Tool->new( type => $measure->{type}, from => $measure->{from}, to => $measure->{to}, ); $converter->calculate($measure->{amount}) } # more 'convert' methods omitted }

Replies are listed 'Best First'.
Re: MooseX::Types::TypeDecorator error
by Anonymous Monk on Jul 16, 2009 at 13:13 UTC
    I can't seem to replicate that ; I get
    Global symbol "$measure" requires explicit package name at test.pl lin +e 41
      I can't replicate that too! Though below code is just simplified version of my code, it works fine:
      use MooseX::Declare; class Foo { use feature qw(say); use Data::Dumper; use MooseX::MultiMethods; use MooseX::Types -declare => [qw(Bar MyStr MyNum)]; use MooseX::Types::Moose qw(Str Num); use MooseX::Types::Structured qw(Dict); BEGIN { subtype MyStr, as Str; subtype MyNum, as Num; subtype Bar, as Dict[ string => MyStr, number => MyNum, ], message { 'Invalid Foo struct' }; }; multi method baz(Bar $barrr) { say'Bar'; say Dumper $barrr } multi method baz(Str $string) { say 'Str'; say Dumper $string; } } no Moose; no MooseX::Declare; 1; package main; my $foo = Foo->new; $foo->baz( { string => 'hello', number => 1.00, } ); $foo->baz( 'hello world' )
        Thanks. My problem is i'm missing MooseX::Declare because Devel-Declare-0.005007 won't compile on windows, sorry :)

        You should put your MooseX-Types library into a separate file. The method signatures are parsed at compile time, and if they are defined in the same package, they are not declared yet.


        Ordinary morality is for ordinary people. -- Aleister Crowley

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://780700]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found