in reply to Moose: mutualy exclusive bolean attributes.

This is perhaps a more Moose-ish approach:
#!/usr/bin/perl package Foo; use Moose; use Moose::Util::TypeConstraints; enum 'Status' => qw(success error pending); has 'status' => (is => 'rw', isa => 'Status|Undef'); package main; use strict; use warnings; my $o = Foo->new; foreach (undef, 'success', 'bar', 'error') { $o->status($_); printf "status: %s\n", $o->status; }
See Moose::Util::TypeConstraints

Replies are listed 'Best First'.
Re^2: Moose: mutualy exclusive bolean attributes.
by ikegami (Patriarch) on Dec 23, 2010 at 15:58 UTC

    It's worth noting that your method doesn't prevent individual accessors.

    has 'status' => ( is => 'rw', isa => 'Status|Undef', handles => { success => sub { ( $_[0]->status() // '' ) eq 'success' }, error => sub { ( $_[0]->status() // '' ) eq 'error' }, pending => sub { ( $_[0]->status() // '' ) eq 'pending' }, }, );

      Thank you. ikegami

      That looks like it handles the requirement for a getter for the success|error|pending attributes. I guess if I want a setter as well, I can expand those subs to larger ones that read the value of $_[1] as well.

      Combined with Arunbear's suggestion to use Moose::Util::TypeConstraints to get an enum on the underlying status field, I think I am three quarters of the way to what I want to achieve.

        I guess if I want a setter as well, I can expand those subs to larger ones that read the value of $_[1] as well.

        That doesn't make much sense. What would happen for

        $self->error(0);