http://qs1969.pair.com?node_id=906001


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

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.

Replies are listed 'Best First'.
Re^5: Inherit from a Role (or something like that)
by John M. Dlugosz (Monsignor) on May 21, 2011 at 12:41 UTC
    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.