philippe44 has asked for the wisdom of the Perl Monks concerning the following question:

Hi - I'm green to Perl and I'm having problem understanding some inheritance behavior. Maybe it's obvious but I'm missing it (I used to be C++)
The class structure is the following: I have 2 classes built on top of each other, on top of Socket IO::Socket::INET ==> remote ==> top
'remote' defines 2 methods : 'open' and 'request'
'top' overloads 'open' and add methods 'new' that is called by the application actually using it and a method 'sysread' to overload default read
#pseudo code in class 'top' sub new { my $class = shift; . . return open($class , $some_args); } sub open { my $class = shift; . . # I guess this is where the blessing of to $class happens ? my $sock = $class::SUPER->new($some_args); . . return $sock->request($some_args); } sub sysread { my $class = shift; . . $class::SUPER->read($var, 20); . . return $sock->request($some_args); } #pseudo code in class 'remote' sub sysreadline(*;$) { my ($handle, $maxnap) = @_; . $handle = qualify_to_ref($handle, caller()); sysread($handle, $var, 10); . } sub request { my $self = shift; . sysreadline($self, 10); . }
What happens is that a call to "top::new", calls "top::open" that calls "remote::request" as expected and then in 'sysreadline', the call to 'sysread' calls IO::Socket::INET::sysread, not 'top::sysread'. I thought in IO::Socket::INET::new, the object was blessed as a 'top' so 'sysread' would call the overloaded one
After that, all subsequent call to 'sysread' by the application with a 'top' object call the overloaded 'sysread'
(NB: my "real" problem is about a multiple inheritance, but I'm trying first to understand a basic behavior here, sorry)

Replies are listed 'Best First'.
Re: inheritance problem
by hippo (Archbishop) on Oct 06, 2015 at 20:03 UTC

    Within your sub sysreadline the call to sysread is just a a plain subroutine call and not a method call. What happens if you make it a method call instead?

      understood but so if the plain sysread is called, then what confused me a lot is that another app required a derivative of this code where SSL must be used to 'top' is inheriting from both 'remote' and 'IO::Socket::SSL' (I had to do that because I cannot modify the 'remote' class, I can only touch the descendant 'top') class
      base qw (IO::Socket::SSL remote);
      With that, method calls will always try to be resolved in IO::Socket::SSL first, before going to IO::Socket::INET - this might be ugly and there might be a clean way to do that, I just don't know
      But although this works, now what happens is that the plain sysread (made in sysreadline) uses the overloaded sysread method in 'top'???
        bummer ... got it now, it was a side-effect of adding a SSL socket. The calls to top::sysread were not made by 'request' but were made by the SSL negotiation !
Re: inheritance problem
by GotToBTru (Prior) on Oct 06, 2015 at 19:56 UTC

    Try to create the shortest possible example that compiles and demonstrates the issue.

    Dum Spiro Spero
Re: inheritance problem
by Anonymous Monk on Oct 07, 2015 at 01:15 UTC
    You're trying to use inheritance, that is your problem :)