Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by BrowserUk (Pope)
on Jun 27, 2013 at 11:42 UTC ( #1040993=note: print w/ replies, xml ) Need Help??


in reply to Re: OO: how to make a generic sub (use roles or inheritance)
in thread OO: how to make a generic sub

tobyink++ really useful post. (You should think about putting several of your recent OO-related posts together into YAOO-Tutorial!)

I think its is worth noting that Roles can be simply implemented using basic Perl5 mechanisms (and with less loadtime/runtime costs):

#! perl-slw use strict; use 5.010; { package Roles::Jumper; ##requires "name"; sub import { my $caller = caller; { no strict; die "'name' method required" unless exists ${ "$caller\:\: +"}{name}; *{ "$caller\:\:jump" } = \&jump; } return; } sub jump { my $self = shift; say $self->name, " jumps!"; } } { package Pet::Dog; ##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 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; __END__ C:\test>roles.pl Fido jumps! Fido says woof! Felix jumps! Felix says meow!

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.


Comment on Re^2: OO: how to make a generic sub (use roles or inheritance)
Download Code
Re^3: OO: how to make a generic sub (use roles or inheritance)
by tobyink (Abbot) on Jun 27, 2013 at 12:25 UTC

    "I think its is worth noting that Roles can be simply implemented using basic Perl5 mechanisms"

    Ultimately all of Moose can be implemented using basic Perl5 mechanisms. (Because all of Moose is implemented using basic Perl5 mechanisms!)

    Yes, this aspect of roles is basically just importing. But Role::Tiny also gives you method conflict checking out of the box (i.e. if you compose two roles with the same method name into the same class, you don't get a random choice, you get an error). And it provides nice integration with Class::Method::Modifiers if you happen to have it installed.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      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:

      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
Node Status?
node history
Node Type: note [id://1040993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-09-21 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls