Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: construct a standard object oriented program

by chhe (Sexton)
on Mar 30, 2002 at 10:33 UTC ( [id://155419]=note: print w/replies, xml ) Need Help??


in reply to construct a standard object oriented program

Basically the perl features specific to object-orientation are not many. The rest is a application of a collection of conventions, style and idioms, which is why from my point of view the perl approach is so exciting.

To define a class in perl from which objects can be created and against which functions can be called, you basically need a perl module with a constructor function, which can be named as you like - conventionally "new" -, you need to "bless" a datastructure of the module and return the "blessed" datastructure from the constructor. The returned "blessed" datastructure, can now be treated like a "object", in such that functions can be called upon it. The "methods" or functions of a blessed "Class" get the instance data of the object always a first parameter.

The rest is as said very a matter of style and idioms, also inheritance, which is not specifically supported by perl, but with a more general approach, through the @ISA mechanism, where when a function is not found the "objects" package it looks up the @ISA variable for other packages to lookup.

The fact that much of perls object-orientation is very much a matter of style and idioms is a powerful feature, because you are flexible in terms of design decisions: You make decisions very much depending in the context of your application needs. There are idioms for: different ways of representing an object, addressing privacy, automatically generating accessors methods, object destruction, functions being both class and instance methods, efficient ways of storing attribut data, debugging, loading objects at runtime, runtime dispatch of function calls etc.

Also there are modules, which support some of these idioms ready to use, like ObjectTemplate and others.

All this has been decribed, as my colleque commentors have already observed, in other places much better.

  • Comment on Re: construct a standard object oriented program

Replies are listed 'Best First'.
Re: Re: construct a standard object oriented program
by orkysoft (Friar) on Mar 30, 2002 at 16:10 UTC

    To define a class in perl from which objects can be created and against which functions can be called, you basically need a perl module with a constructor function,

    No, you need a namespace -- defined with packagename;

    I've defined a class (albeit a very, very small one) inside of a larger file once.

    Lur: "But if this cape shrinks, consider your species extinct!"

      Correct, that wasn't precise: you can have as many classes you want in a file - the reason for bless taking an optional additional parameter...., well i said basically....

      Chris

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found