Skip to content

Instantly share code, notes, and snippets.

@blabos-zz
Created May 7, 2021 13:38
Show Gist options
  • Save blabos-zz/da3ad7ee4e9334c268cf224428fe1818 to your computer and use it in GitHub Desktop.
Save blabos-zz/da3ad7ee4e9334c268cf224428fe1818 to your computer and use it in GitHub Desktop.
package Parent;
use Moo;
has 'parent' => ( 'is' => 'ro' );
sub core_attrs {
my ($self) = @_;
my $specs = 'Moo'->_constructor_maker_for('Parent')->all_attribute_specs;
my @attr_names = keys %{$specs};
return ( map { $_ => $self->$_ } @attr_names );
}
##############################################################################
package Foo;
use Moo;
extends 'Parent';
has 'foo' => ( 'is' => 'ro' );
##############################################################################
package Bar;
use Moo;
extends 'Parent';
has 'bar' => ( 'is' => 'ro' );
##############################################################################
package main;
use v5.32;
my $f = Foo->new( parent => 42, foo => 123 );
my $b = Bar->new( $f->core_attrs, bar => '456' );
use Data::Dumper;
say Dumper( $f, $b, { $f->core_attrs } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment