Moose supports default values ββfor attributes, for example:
has 'foo' => ( is => 'rw', isa => 'Int', default => 42 );
But if you do not want to follow the Muse route, an easier way to achieve what you want:
sub new { my ( $package, %config ) = @_; my %defaults = ( x => 'a', y => 'b' ); my $self = { %defaults, %config };
Since specifying the same hash key twice in the hash initialization will hit the first one, any keys in %config will simply override those in %defaults .
friedo
source share