Question

I'm trying to use an alias with MooseX::Aliases and MooseX::Constructor::AllErrors

However, the two don't seem to play nicely together. Consider the following example:

package Alias
{
  use Moose;
  use MooseX::Aliases;
  use MooseX::Constructor::AllErrors;

  has foo => (
    is => 'rw', isa => 'Str', required => 1, alias => 'bar'
  );
}

use strict;
use warnings;
use Alias;

my $obj;

eval {
  $obj = Alias->new( bar => 'alias_value' );
};

if ($@)
{
  foreach my $error ( $@->errors )
  {
    print $error ."\n";
    print $error->message ."\n";
  }

  exit 1;
}

print $obj->bar ."\n";
$obj->foo( 'new_alias_value' );
print $obj->foo."\n";

1;

This should allow me to create an Alias object using the 'bar' alias... shouldn't it? Does anyone know if MooseX::Constructor::AllErrors is supposed to support aliased attributes?

Était-ce utile?

La solution

It's a bug, in that it violates expectations, but it's not easily resolvable -- the problem is that MooseX::Aliases modifies what arguments are allowed/accepted in the constructor, but MooseX::Constructor::AllErrors is not aware of this, so when it looks at the passed values at construction time, it errors out when there is no 'agency' field.

This gets around the situation by manually moving the aliased field before MooseX::Constructor::AllErrors sees it:

around BUILDARGS => sub {
    my $orig = shift;
    my $self = shift;
    my %args = @_;

    $args{agency} //= delete $args{company};

    $self->$orig(%args);
};

The good news is that this has hope of working better in the future, because there are plans for MooseX::Aliases to be cored, which would force all other extensions (e.g. MXCAE) to support the alias feature properly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top