I used perl today. Moose. I ran into a problem. It was annoying. I am way too stressed and tired to blog more. But I will anyway, secure in the knowledge that no one reads this blog but google’s spiders.
Okay anyway I used MooseX::Declare, and couldn’t get the method signature stuff to work. I did something like
method weekend_or_vacation (DateTime $dt){ # check if weekend or vacation # with vacation being the tricky bit if($vacation || $weekend){ return 1; }else{ return 0; } }
But MooseX::Declare kept complaining that it didn’t know what DateTime was. I scanned the tests in t and sure enough, they all test simple things like Str and ArrayRef and so on, but none of the more magical parts of type checking.
I eventually solved it the old fashioned way by puking if the argument wasn’t a DateTime, but I’d rather do it the method signature way.
I suspect you’ll need to pre-declare the DateTime type using Moose::Util::TypeConstraints ‘class_type’ function.
You need to do:
use Moose::Util::TypeConstraints;
class_type ‘DateTime’;
no Moose::Util::TypeConstraints;
to define it in such a way that MX::D knows about it.
You probably need to declare DateTime as a class_type with Moose::Util::TypeConstraints. In the future, feel free to ask on irc://irc.perl.org/#moose
You want MooseX::Types::DateTime (http://search.cpan.org/perldoc?MooseX::Types::DateTime)
class Foo {
use MooseX::Types::DateTime qw(DateTime);
method weekend (DateTime $dt) { }
}
You also get a couple of coercions for free that way, so you could say:
method vacation (DateTime $dt does coerce) { }
With that, you could call vacation with epoch values or hashrefs defining the datetime, for example:
$foo->vacation( time() );
# or
$foo->vacation( { year => 2010, month => 6, day => 1 } );
You can fork MooseX::Declare on http://github.com/rafl/moosex-declare and add a test for your case.
This is stupidly simple (so stupidly simple that I’m not sure why it’s not done automatically), but…
The issue is that MX::Declare doesn’t know anything about a class named “DateTime”.
Add a “use DateTime;” and it should work.
You need to declare DateTime as a type to be able to use it as such. Please see `perldoc Moose::Manual::Types`
:)
The error message probably told you that a type called ‘DateTime’ wasn’t declared. Either declare it, for example using class_type from Moose::Util::TypeConstraints, or just use one of the predefined moose types for DateTime from cpan, for example via MooseX::Types::DateTime.
If you’ve got any suggestions on how to make the error message clearer, I’d love to hear them!
Thanks to all for the help. As I wrote my most recent post, using MooseX::Types::DateTime did the trick.
As to the error message, I also detailed how I tried to do what the error message told me to do… Perhaps that will help you make the error message clearer—seeing all of the ways a newcomer fails to get it right. In short, I was unable to declare DateTime as a type in a way that MooseX::Declare’s parsing of method signatures was able to understand. I also sifted through the code and tests of MooseX-Method-Signatures but was unable to find examples that were similar or insights into how to declare non-standard types for use by method signatures.
Alexandr Ciornii, I’ve already cloned the code, I’m just not sure what to test just yet, as I feel like I’m just missing the correct way to invoke class_type such that MooseX::Declare pays attention when it parses method signatures.
Dave Rolsky, perhaps I will hop on irc as I begin to use Moose more. But I’ve never really used irc, other than participating in a Fluid code sprint once.