Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

MooseX::Declare and undef attributes - MooseX::UndefTolerant not working

by err (Novice)
on Sep 24, 2010 at 10:55 UTC ( [id://861781]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there,

I'm writing code using MooseX::Declare.
Now I experienced some trouble when initializing a new object using:

my $f = new Foo( one => 'valued', two => undef );

Foo.pm

use MooseX::Declare; class Foo { has 'one' => (is => 'rw', isa => 'Str'); has 'two' => (is => 'rw', isa => 'Str'); };

moose complains that two isn't the type which expected.


Short, it happens what MooseX::UndefTolerant tries to avoid. But this module does nothing if I use MooseX::Declare to write a class. I tried both ways, the standard way to make all attributes undef tolerant and the other way using traits. But both had no effect.

So anyone can help me? Thank you!

Replies are listed 'Best First'.
Re: MooseX::Declare and undef attributes - MooseX::UndefTolerant not working
by ikegami (Patriarch) on Sep 24, 2010 at 14:09 UTC
    undef isn't a string. If undef is an acceptable value, you want Maybe[Str] or Undef|Str.

      I think maybe does the job. For the standard Moose types it works well like isa => 'Maybe[Str]'. The problem is I created some subtypes for my needs.

      package Foo::Types::Library; use Data::Dumper; use MooseX::Types -declare => [ qw/ GooglePrimaryEmail WWWGoogleContactsTypeEmail / ]; use MooseX::Types::Email qw/EmailAddress/; # WWWGoogleContactsTypeEmail class_type WWWGoogleContactsTypeEmail, { class => 'WWW::Google::Contacts::Type::Email', }; # GooglePrimaryEmail subtype GooglePrimaryEmail, as EmailAddress, message { 'validation failed for: '. Dumper $_; }; coerce GooglePrimaryEmail, from ArrayRef[WWWGoogleContactsTypeEmail], via { my $p = (grep { $_->{primary} } @$_)[0]; $p = $p ? $p->{value} : @$_->[0]{value}; return $p; };

      This works for something like:

      use MooseX::Declare; class Foo { use Foo::Types::Library qw /GooglePrimaryEmail/; has 'email' => ( is => 'rw', isa => GooglePrimaryEmail, coerce => 1 ); }

      Works fine unless email is initialized undef. In this case the attribute check fails.
      Now maybe solves this but I don't know how I can create my own Maybe. I use MooseX::Types for creating my own types. As I can't see any syntax to create my own Maybe type I used the one in Moose::Util::TypeConstraints:

      maybe_type (GooglePrimaryEmail);

      I added this line to my type library, exported it with declare as Maybe[GooglePrimaryEmail and tested it with:

      use MooseX::Declare; class Foo { use Foo::Types::Library qw /Maybe[GooglePrimaryEmail]/; has 'email' => ( is => 'rw', isa => 'Maybe[GooglePrimaryEmail]', coerce => 1 ); }

      Now every attribute type check fails. :(

      Any help?

        Maybe[...] doesn't work with coercion (wontfix), but ...|Undef does (although it was buggy before 1.11).

Re: MooseX::Declare and undef attributes - MooseX::UndefTolerant not working
by bobr (Monk) on Sep 25, 2010 at 18:08 UTC
    Hi,

    I played a bit with both plain Moose variant and MooseX::Declare one. I used Devel::Trace output to see what happens behind scenes.

    It looks like initialize_instance_slot for attributes is not called when MXD is used -- the MooseX::UndefTolerant::Attribute is using it to plug its behavior. I have no idea why it is, though. Might be just bug on MXD part.

    I've seen quite a number of strange behavior (for instance with use overload) with MXD that I switched to plain Moose some time ago. Not only it get large speedup, there are also less surprises.

    -- regards, Roman

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-19 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found