OS Manuals

MooseX::OneArgNew(3pm)

MooseX::OneArgNew(3) User Contributed Perl Documentation MooseX::OneArgNew(3)

In our class definition:

  package Delivery;
  use Moose;
  with('MooseX::OneArgNew' => {
    type     => 'Existing::Message::Type',
    init_arg => 'message',
  });
  has message => (isa => 'Existing::Message::Type', required => 1);
  has to => (
    is   => 'ro',
    isa  => 'Str',
    lazy => 1,
    default => sub {
      my ($self) = @_;
      $self->message->get('To');
    },
  );

When making a message:

  # The traditional way:
  my $delivery = Delivery->new({ message => $message });
  # or
  my $delivery = Delivery->new({ message => $message, to => $to });
  # With one-arg new:
  my $delivery = Delivery->new($message);

MooseX::OneArgNew lets your constructor take a single argument, which will be translated into the value for a one-entry hashref. It is a parameterized role with three parameters:

You can apply MooseX::OneArgNew more than once, but if more than one application's type matches a single argument to "new", the behavior is undefined and likely to cause bugs.

It would be a very bad idea to supply a type that could accept a normal hashref of arguments to "new".

2026-06-28 perl v5.42.2

perl-moosex-oneargnew 0.007-r4 · os · ingested 2026-09-30