Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Inherit from a Role (or something like that)

by John M. Dlugosz (Monsignor)
on May 20, 2011 at 23:37 UTC ( #905995=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Inherit from a Role (or something like that)
in thread Inherit from a Role (or something like that)

So Server and Client are both different from each other (though may share some stuff), and SSH and Socket are also different from each other but share a common interface.

So, Server and Client (or their base class) "has" (not "is"!) a Connection. A Connection may be different concrete classes, or may be a single class that "has" a BackEnd. Note that in Perl you are more flexible in not having to define an "Interface" or have explicit base/derived relationships. Each concrete class can be implemented in any way it likes.


Comment on Re^3: Inherit from a Role (or something like that)
Re^4: Inherit from a Role (or something like that)
by elTriberium (Friar) on May 21, 2011 at 00:15 UTC
    Currently I only have the SSH module and now Server / Client "is" a SSH connection. The benefit of that is that I can just do things like:
    package SSH; has username => (is => 'rw', isa => 'Str'); sub connect { [...] $self->send($self->username); } package Client; extends qw(SSH); has username => (... default => 'admin'); package Server; extends qw(SSH); has username => (... default => 'root'); package main; my $client = Client->new(); my $server = Server->new(); $client->connect(); $server->connect();
    It's automatically using the correct username, because I'm overwriting this attribute of the SSH class in Client and Server. So, the point is, with SSH alone it works perfectly fine. I just have trouble adding the Socket alternative.
      So that's why you don't want to derive from it ("isa"), but contain one. Use delegation to expose the calls in the same manner that inheritance would. I do agree that passing the parameter in to create the contained object will be more work since something like that is not handled automatically. So far I've avoided that in my current project, but have speculated on passing the whole parameter list given to 'new' in to the builders for the helper objects, so each pulls the parameters it wants.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-02 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls