Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^4: OO: how to make a generic sub (use roles or inheritance)

by BrowserUk (Patriarch)
on Jun 27, 2013 at 20:45 UTC ( [id://1041096]=note: print w/replies, xml ) Need Help??


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.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1041096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found