[Melbourne-pm] Moose 'isa' property prevents setting attribute to undef

David Warring david.warring at gmail.com
Sun Sep 16 20:19:19 PDT 2012


Hi Ian,
You most likely want to delete the attribute entirely, rather than setting
it to undef.

One way is to define a clearer property for it:

package Foo;

use Moose;

# ...

has 'dataset' => (
    is => 'rw',
    isa => 'AG::Dataset',
    required => 0,
    clearer => 'clear_dataset',
);

then ...

my $foo = Foo->new();
$foo->dataset(bless {}, 'AG::Dataset');

use YAML::Syck; print YAML::Syck::Dump({foo => $foo});

$foo->clear_dataset;

use YAML::Syck; print YAML::Syck::Dump({foo => $foo});

- David

On Mon, Sep 17, 2012 at 12:52 PM, Ian Macdonald <ickphum at gmail.com> wrote:

> Hi,
>
> We've got a Moose class with this attribute:
>
>
> has 'dataset' => (
>     is => 'rw',
>     isa => 'AG::Dataset',
>     required => 0,
> );
>
> It's not required so you can happily create objects without supplying a
> value for it, but once you've done so, you can't then remove/clear it via
> "$object->dataset(undef)" :
>
>     Attribute (dataset) does not pass the type constraint because:
> Validation failed for 'AG::Dataset' failed with value undef (not isa
> AG::Dataset)
>
> Given that it's legal to have undefs in there initially it seems funny not
> to be allowed to revert to that state. Am I missing another property which
> will allow this? I'd rather not have to remove the validation or do it
> manually, seems like a backward step.
>
> Thanks,
> --
> Ian Macdonald
>
>
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20120917/8e9ad50c/attachment.html>


More information about the Melbourne-pm mailing list