On Thu, Dec 10, 2009 at 10:41 AM, Madison Kelly <span dir="ltr"><<a href="mailto:linux@alteeve.com">linux@alteeve.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Anywho, I've been re-reading the (sparse) Net::DBus docs and it always shows it's constructor using the "super" method. I know that this has to do with inheritance, but to be honest, I've never really understood the true meaning of this.<br>
</blockquote><div><br>hi madison,<br><br>perhaps this will help you understand a bit more about what SUPER does, and how inheritance works in perl.<br><br>####[ START Foo.pm ]###<br><font face="courier new,monospace">{ package Foo;<br>
sub new { bless {}, shift }<br> sub says { print shift()->word, "\n" }<br> sub word { 'foo!' }<br>1 }<br>{ package Foo::Bar;<br> use base qw(Foo);<br> sub word { 'bar!' }<br>1 }<br>
{ package Foo::Bar::Baz;<br> use base qw(Foo::Bar);<br> sub says {<br> my $self = shift;<br> print __PACKAGE__, " isa Foo\n" if $self->isa('Foo');<br> print 'Foo::Bar says ', $self->SUPER::word, "\n",<br>
__PACKAGE__, ' says ', $self->word, "\n";<br> }<br> sub word { 'baz!' }<br>1 }</font><br>####[ END Foo.pm ]###<br><br><span style="font-family: courier new,monospace;"><span style="color: rgb(255, 0, 0);">bash$</span> perl -MFoo -e 'Foo->new->says'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">foo!</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"><span style="color: rgb(255, 0, 0);">bash$</span> perl -MFoo -e 'Foo::Bar->new->says'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">bar!</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"><span style="color: rgb(255, 0, 0);">bash$</span> perl -MFoo -e 'Foo::Bar::Baz->new->says'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Foo::Bar::Baz isa Foo</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Foo::Bar says bar!</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Foo::Bar::Baz says baz!</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace; color: rgb(255, 0, 0);">bash$ </span><br style="font-family: courier new,monospace;">
<br>as a general rule though, it's best to avoid "implementation inheritance" ("use base", "push @ISA", "use parent") as much as humanly possible. if at all possible it's much better in practice to use "interface inheritance" ["use Module qw( some interfaces to import )"]. inheritance is intrinsic to OOP, so it's important to understand the design patterns associated with it and the pros and cons of each.<br>
<br>cheers,<br>--<br>Shaun Fryer<br></div></div>