Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: kind of effort required for learning OO perl ?!

by InfiniteSilence (Curate)
on Jul 29, 2010 at 18:50 UTC ( [id://851992]=note: print w/replies, xml ) Need Help??


in reply to kind of effort required for learning OO perl ?!

According to most online sources OO programming involves three basic elements: inheritance, polymorphism, and data encapsulation (see references included in the code). Some examples of basic ways to achieve these are below.
#... http://www.cs.mun.ca/~donald/bsc/node12.html # INHERITANCE package dog; sub new { my $class = shift; return(bless({'puppies'=>15},$class)); } sub get { my ($self,$name) = @_; return ($self->{$name}); } sub set { my ($self, $name, $newvalue) = @_; #this is silly but saves space $self->{$name} = $newvalue; return $self->{$name}; } sub bark { return 'woof...I INHERITED this capability.'; } package cockerSpaniel; use parent -norequire, qw|dog|; sub new { my ($class) = @_; return(bless(dog->new(),$class)); } sub bark { my ($class) = @_; return ($class->SUPER::bark() . '..but then overrode it'); } package main; my $doggie = new cockerSpaniel(); $doggie->set('puppies',29); print qq|\nHere are the cockerSpaniel's puppies: | . $doggie->get('pup +pies'); my $realdog = new dog(); print qq|\nHere are the parent dog's puppies: | . $realdog->get('puppi +es'); # POLYMORPHISM, or dynamic binding of function calls print qq|\n| . $doggie->bark(); # Data Encapsulation # There are numerous techniques. Take your pick: # http://www.csse.monash.edu.au/~damian/TPC/1999/Encapsulation/Paper.h +tml
The above, with the reading, should take only about an hour or so to learn. It is good, in my opinion, to learn the old non-Moose way of defining classes since there is so much code out there that uses these techniques. The problem that I have seen with OO programming in general has little to do with the techniques or semantics -- it has to do with learning to think in an object oriented way. For this I recommend using something entirely outside of the scope of a particular language. I recommend reading Object Thinking by David West (don't worry that is was published by Microsoft Press...this book was published under an experiment where no Microsoft technologies were discussed in the book). Here you will learn not just terminology and the mechanics of how to migrate from top-down to OO, but how to use nomenclatures and tools (CRC cards, for instance) that will help you describe and analyze problems before you write code.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: kind of effort required for learning OO perl ?!
by chromatic (Archbishop) on Jul 29, 2010 at 19:25 UTC

    I can't recommend some of this advice; it looks prone to making messes.

    package dog;

    Lowercase module names are, by convention, pragmas.

        return(bless(dog->new(),$class));

    Why double bless? If the parent constructor is at all sane, it allows subclassing.

    my $doggie = new cockerSpaniel();

    The indirect method invocation syntax is fraught with peril; it's ambiguous to parse and Perl occasionally guesses incorrectly depending on the compilation order of your code.

    Also inheritance is not an essential part of object orientation. Arguably neither is encapsulation.

    You're absolutely right about object design being important. Most tutorials never teach that, and it's vital to writing effective OO.

      What double bless...are you referring to this:
      sub new { my ($class) = @_; return(bless(dog->new(),$class)); }
      ?

      I borrowed this syntax from the perlbot example:

      sub new { my $type = shift; my $self = Bar->new; $self->{'biz'} = 11; bless $self, $type; }
      When I checked that $self reference was indeed blessed before it was passed through bless again:
      DB<3> p UNIVERSAL::isa($self,'Bar'); 1
      What is the harm?

      Celebrate Intellectual Diversity

        What does dog->new() return? If you didn't have any new() defined in Bar, what would Bar->new() return?

        I borrowed this syntax from the perlbot example

        Thanks. I'll submit a patch to fix that example code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found