#! perl-slw use strict; use 5.010; { package Roles::Jumper; ##requires "name"; sub checkISA { no strict 'refs'; my( $caller, $method ) = @_; return $caller if exists ${ "$caller\:\:" }{ $method }; for my $parent ( @{ "$caller\:\:ISA" } ) { return $caller if exists ${ "$parent\:\:" }{ $method }; return $_ while $_ = checkISA( $parent, $method ); } return; } sub import { my $caller = caller; { no strict 'refs'; die "'name' method required" unless exists ${ "$caller\:\:" }{name}; die "'jump' already exists in package '$_'" while $_ = checkISA( $caller, 'jump' ); *{ "$caller\:\:jump" } = \&jump; } return; } sub jump { my $self = shift; say $self->name, " jumps!"; } } { package Jumper; sub jump { say 'How high?'; } } { package Pet::Dog; ## our @ISA = qw[Jumper]; ## uncomment to test checkISA; ##use Role::Tiny::With; ##with "Roles::Jumper"; #require Roles::Jumper; ## used if package in separate file Roles::Jumper->import; sub new { my $class = shift; bless {@_}, $class; } ## sub jump { say 'How high?'; } ## uncomment to test checkISA sub name { my $self = shift; return $self->{name}; } sub sound { my $self = shift; say $self->name, " says woof!"; } } { package Pet::Cat; ##use Role::Tiny::With; ##with "Roles::Jumper"; #require Roles::Jumper; ## used if package in separate file Roles::Jumper->import; sub new { my $class = shift; bless {@_}, $class; } sub name { my $self = shift; return $self->{name}; } sub sound { my $self = shift; say $self->name, " says meow!"; } } my $fido = Pet::Dog->new(name => "Fido"); $fido->jump; $fido->sound; my $felix = Pet::Cat->new(name => "Felix"); $felix->jump; $felix->sound;