I used perl today, and I can’t figure out how to get my paper man icon.

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.

Advertisements

11 thoughts on “I used perl today, and I can’t figure out how to get my paper man icon.

  1. I suspect you’ll need to pre-declare the DateTime type using Moose::Util::TypeConstraints ‘class_type’ function.

    use MooseX::Declare;
    use Moose::Util::TypeConstraints;
    use DateTime;
    class_type DateTime;
    
    class MyApp { 
        method weekend_or_vacation (DateTime $dt) { ... }
    }
    
  2. 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.

  3. 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 } );

  4. 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.

  5. You need to declare DateTime as a type to be able to use it as such. Please see `perldoc Moose::Manual::Types`

    :)

  6. 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!

  7. 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.

  8. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s