Re: check arguments for ->new

[email protected] ("Ian Sillitoe")
Newsgroups perl.moose
Message-ID <[email protected]>
> Any clever idea how I can make an alias attribute?


Hmmm... there may be a simple way of doing it but I can't see anything
obvious on the docs - here's a workaround hack:


  package My::App;

  use Moose;

  has 'colour' => (
      is      => 'rw',
      isa     => 'Str',
  );

  sub BUILDARGS {
      my $class = shift;
      my $args  = @_ == 1 ? $_[0] : { @_ };

      # deal with 'color' in constructor
      if ( exists( $args->{color} ) ) {
          $args->{ colour } = $args->{color};
          delete $args->{ color };
      }

      return $args;
  }

  # manually setup accessor for color (redirect to colour)
  sub color {
      my $self = shift;
      my $colour = shift;
      $self->colour( $colour ) if @_ > 1;
      return $self->colour;
  }


I'm guessing here - but if something like this hasn't already been written
then I guess you should be able to register your own attribute trait (e.g.
called 'alias') and deal with it in the BUILD method of the trait role, e.g.


http://search.cpan.org/~drolsky/Moose-0.61/lib/Moose/Cookbook/Meta/Recipe3.pod


Cheers,

Ian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.