in reply to Re^3: OO: how to make a generic sub (use roles or inheritance)
in thread OO: how to make a generic sub
Ultimately all of Moose can be implemented using basic Perl5 mechanisms. (Because all of Moose is implemented using basic Perl5 mechanisms!)
Yes, but it was the "simply implemented " bit I was drawing attention to.
It begs the question about the weight of Moose.
But Role::Tiny also gives you method conflict checking out of the box
That also can be done lightly:
#! 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 $_ = che +ckISA( $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;
It's not that it isn't convenient to have these small things done via a module; it's that whenever I peek inside those modules, they always seem to be implemented in such complex fashion. Both in their own code and that of the number and weight of their dependencies. It just all adds up to make things slow and cumbersome.
|
|---|