Alister West

home is where your code is ...

Perl Moose Example 1

Moose constructor with coercion to object

package Bar;
use Moose;
use Moose::Util::TypeConstraints;

coerce 'Bar' => from 'HashRef' => via { Bar->new( %{$_} ) };

has 'Username' => ( is => 'ro', isa => 'Str' );
has 'Password' => ( is => 'ro', isa => 'Str' );

package Foo;
use Moose;
has 'Auth' => ( is => 'rw', isa => 'Bar', coerce => 1 );

package main;
use feature qw/say/;
my $foo = Foo->new( Auth => { Username => 'my_username', Password => 'my_password' } );
printf "Username: %s, Password: %s\n", $foo->Auth->Username, $foo->Auth->Password;
By Alister West