The heart of the big benefits from OO is encapsulation, not inheritance.Interesting observation, open to debate. I am new to OO, and intuitively, it is inheritance that I see as a bigger benefit than any other OO properties. In fact, intuitively, inheritance is what I understand better than encapsulation or polymorphism, etc.
Yes, multiple inheritance (MI) can cause a lot of problems, especially if using modules that were built by others, and hence, were not designed from ground up to be inherited from in conjunction with other modules. But life is complicated, and more often than not involves MI. After all, we all inherited from a dad and a mom, with hopefully no clashes and dire consequences.
That said, should not my approach, stated in my second post above, avoid the problems of same-named methods clashing and overriding each other? Except, it doesn't work all the way. Here is the real code I am trying out. I want to use a config file and connect to an email server. For this, I use Config::Simple and Mail::IMAPClient. Thus far I was using it normally and it works. Lately I decided to OO-ize it thusly --
package Mypackage; use Config::Simple; use Mail::IMAPClient; use strict; sub new { my ($class) = @_; bless {}, $class; } sub load_config { my ($self, %a) = @_; return new Config::Simple("$a{cfg}"); } sub connect_to_imaphost { my ($self, %a) = @_; return Mail::IMAPClient->new( Server => $a{IMAP_HOST}, User => $a{EM_UNAME}, Password => $a{EM_PWD}, Uid => 0, Debug => $a{DEBUGGING}, ); } sub other_methods... {} ### and then, in my script... use Mypackage; my $a = Mypackage->new(); # $a has all the methods of Mypackage $f->load_config(cfg => "my.conf")->import_names(); # this works and imports all the config vars my $imap = $a->connect_to_imaphost( server => $IMAP_HOST, user => $EM_UNAME, password => $EM_PWD, debug => $DEBUGGING, ); my $msg_count = $imap->message_count("$INBOX"); # the vars above have been imported via Config::Simple # the above, however, fails with the following ##Not connected at ./myscript.pl line 48 ##Error sending '1 STATUS inbox (MESSAGES)' to IMAP: at ./myscript.pl + line 48
In the above code, instances of B and C are made only when called for. In a way, I have achieved encapsulation, and made A inherit the properties of both B and C, but only when desired. Thereby, hopefully, avoiding any problems.
Question is -- is the above code ok? If yes, why is it not working. If no, what am I doing wrong?
Many thanks.
In reply to Re^2: Understanding 'Multiple Inheritance'
by punkish
in thread Understanding 'Multiple Inheritance'
by punkish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |