Inheritance.pl


package Base;

use Moose;

has first  => ( is => 'rw', lazy => 1, default => sub { print 'First' . "\n" } );
has second => ( is => 'rw', lazy => 1, default => sub { print 'Second' . "\n" } );
has third  => ( is => 'rw', lazy => 1, default => sub { print 'Third' . "\n" } );
has forth  => ( is => 'rw', lazy => 1, default => sub { print 'Base Forth' . "\n" } );

###############################################################################

package Child;

use Moose;

extends 'Base';

before 'first' => sub {
    my ($self) = @_;
    print 'Before first' . "\n";
};

after 'second' => sub {
    my ($self) = @_;
    print 'After second' . "\n";
};

around 'third' => sub {
    my ( $next, $self ) = @_;

    print 'Around third' . "\n";
    $next->();
    print 'Around third' . "\n";
};

override forth => sub {
    my ($self) = @_;
    super();
    print 'New Forth' . "\n";
};

###############################################################################

package main;

my $child = Child->new();

$child->first();

print "\n";

$child->second();

print "\n";

$child->third();

print "\n";

$child->forth();