Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to learn Object Oriented programming in Perl ?

by marscld (Beadle)
on Apr 22, 2008 at 08:03 UTC ( [id://682105]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, Trying to rebuild Perl application to Object Oriented style, however, not so sure on how to write good Perl OO program. Previously, I have learned some OO programming knowledge in other programming language. Would you please provide some advice ? Say, which book will be a good reference ?

Replies are listed 'Best First'.
Re: How to learn Object Oriented programming in Perl ?
by moritz (Cardinal) on Apr 22, 2008 at 08:15 UTC
    If you have good OO programming skills already perltoot and perlobj will get you started rather quickly.

    They explain the "blessed hash" objects in Perl very well.

    If that's not enough, read "Object Oriented Perl" by Damian Conway (I haven't read it myself, but it's been recommended here a couple of times).

    Update: See also Really Writing Object Oriented Perl

      If you have good OO programming skills already perltoot and perlobj will get you started rather quickly.
      I was fine with both docs without existing OO programming skills, other than been using some OO Perl modules. But they did make sense only after I covered perlref and perldsc.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: How to learn Object Oriented programming in Perl ?
by elmex (Friar) on Apr 22, 2008 at 08:16 UTC

    I can very much recommend the documentation that comes with perl, there are two tutorials for OO: perldoc perlboot and perldoc perltoot.

    I learned OO in Perl mostly with these. Simply put OO in Perl is just a reference that has a package name attached (via bless (see perldoc -f bless) to it, allowing Perl to find the methods for that 'reference'. So that you can call the subroutines of the package like this: $ref->test_method (1, 2, 3);.

    Hope I didn't confuse you too much now. Just read perlboot and/or perltoot and skim through perlobj for a reference about how to use the OO features of Perl. http://perldoc.perl.org/ is a very useful resource!

Re: How to learn Object Oriented programming in Perl ?
by Erez (Priest) on Apr 22, 2008 at 08:42 UTC

    I always recommend learning by doing. A lot of CPAN modules expose an OO interface, allowing you to instantiate and inherit from them. Using those modules is an excellent way to ease into Perl OOP concepts. After you're there, learning how to create a class, and other OOP concepts used in Perl is much easier.

    Trying to rebuild Perl application to Object Oriented style

    However, this brings up the question, whether you are trying to rebuild an application in OO simply to make it OO, or is there a real need there. If something is already built in a procedural way, and works fine, rebuilding it just to make it OO can cause more harm than profit. Perl have ways to interface with such an application that allows creating OO-based components and you might learn better from interfacing with it whether it should be re-written at all.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

      Thanks for all the reply. You guys have shown a good way for me. Erez, thanks for the reminder. In fact, I'm going to review an existing procedural application and try to make it work in OO way, and to decouple connections between some modules.
Re: How to learn Object Oriented programming in Perl ?
by tirwhan (Abbot) on Apr 22, 2008 at 08:23 UTC

    Before looking for books, try out some of the documentation that comes with Perl (i.e. perldoc). Specifically, look at perlboot, perltoot (probably the most usefuly start for you, since you already know what OO is), perltooc and perlbot. If you'd like something on dead trees I'd recommend starting off with Object Oriented Perl by TheDamian. It's a bit oldish (1999), so it's missing the latest flashy tricks like inside-out objects, but it will give you a solid grounding on how OOP works in Perl 5. Once you've understood that, learning how the new kids do it is easy.

    Oh, and have a look at Moose if you want to see (something similar to) how Perl 6 will be doing OOP.


    All dogma is stupid.
Re: How to learn Object Oriented programming in Perl ?
by Anonymous Monk on Apr 22, 2008 at 08:19 UTC
      Having used both I'd look into Moose rather than Spiffy, Moose being a lot more solid base.

      The nicest thing about Spiffy is the way it handles $self for you. But that's done with a source filter, so... slightly fragile.

      Starting a new project today, I'd go with Moose, and probably try out Method::Signatures. That pretty much brings Perl OO into the Century of the Fruitbat.

      /J

Re: How to learn Object Oriented programming in Perl ?
by strat (Canon) on Apr 23, 2008 at 06:48 UTC

    Good morning marscld,

    in this thread, a lot of links how to do OOP in Perl were posted. But OOP can give you more than just a bunch of coding techniques. If you want to know more about the OOP backgrounds and how to use them, read a good book about object oriented analysis and design (e.g. I like the one written by Grady Booch), and maybe afterwards "Design Patterns" (Gamma/Helm/Johnson/Vlissides), and then maybe "Refactoring" (Fowler).

    These books are not about Perl, but they helped me a lot. I haven't yet found similar books covering Perl. But as far as I can say, they are rather easy to understand, although they use C++ or Java as examples.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2024-03-28 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found