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. :)


In reply to Re: oops concept in perl programming by j3
in thread oops concept in perl programming by Tony1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.