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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: oops concept in perl programming
by ferreira (Chaplain) on Jan 02, 2007 at 18:20 UTC

    There are plenty of material on Perl OO. You may start by the docs in the standard distribution, reading perlobj, perlboot (Tutorial for beginners), perltoot (Tutorial, Part 1), perltooc (Tutorial, Part 2), perlbot (Tricks and Examples). Other monks will provide more references (among books, articles, etc) which can be easier to grok from your perspective of user of other languages.

Re: oops concept in perl programming
by j3 (Friar) on Jan 02, 2007 at 18:52 UTC

    Hi Tony.

    Perl provides very minimal extra magic for object oriented programming. In fact, as I understand it, there's only 3 things Perl does for you to implement OOP:

    1. It provides the `->' syntax, so that when you use that syntax ($some_object->some_method;), perl makes sure to automatically pass the invocant to the method as the first argument.
    2. It provides inheritance via the @ISA package global. You put the names of parent classes into @ISA, and voila, you've just made your class a child of those listed in @ISA. When you call a method that's not in your current class, perl digs around in the ones listed in @ISA to find it.
    3. It provides the bless method to make the above two bits of magic work properly.

    That's basically it. No automatic constructor calls, no automatic object allocation. Your ``object'' is actually (usually) just an anonymous hash which you create in (and is returned by) your `new' class method, and that gets passed around for you when calling methods. The ``instance attributes'' are just keys in that hash, and ``class attributes'' are just lexicals in the module where you write your methods.

    When using objects, as long as you only ever access attributes via methods (and not directly -- which you, of course, can if you like to live dangerously), everything mostly just works like it ought to.

    One last tip: The method that gets called is always looked up via the object reference (that blessed anonymous hash), so even if some base class method calls another base class method, if your own child class has a method of that name, the child class method is the one that'll get used.

    Addendum: If a method is expecting certain instance attributes to exist, you'be better make sure they're there. Methods and instance attributes (the values you have in your anonymous hash) are two totally different things. Welcome to Perl OO: Watch your step. Here's your helmet. :)

Re: oops concept in perl programming
by philcrow (Priest) on Jan 02, 2007 at 19:12 UTC
    While its getting a bit old, Damian Conway's Object Oriented Perl from Manning is a great book to show you the ways of Perl OO.

    Phil

Re: oops concept in perl programming
by bingos (Vicar) on Jan 03, 2007 at 08:41 UTC

    You may also want to have a look at Moose.

Re: oops concept in perl programming
by Codon (Friar) on Jan 03, 2007 at 18:24 UTC
    philcrow mentioned Damian's Book. In addition, you may want to take a look at The Alpaca book by Randal Schwartz. It's a bit newer and sort of eases you in to OO Perl. At least it did in the first edition when it was called Learning Perl Objects, References, and Modules.

    After those two, if you like C++ and Java OO for their strong data encapsulation, Damian has some things to offer on what's recently become en vogue for OO Perl: Inside-Out Objects. Some introduction to this is given in his Best Practices book. He's also got a module or two to help you build these kind of classes.

    Happy Hacking!

    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.