Re: construct a standard object oriented program
by cjf (Parson) on Mar 30, 2002 at 07:53 UTC
|
| [reply] |
Re: construct a standard object oriented program
by rdfield (Priest) on Mar 30, 2002 at 07:45 UTC
|
| [reply] |
Re: construct a standard object oriented program
by chhe (Sexton) on Mar 30, 2002 at 10:33 UTC
|
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.
| [reply] |
|
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!"
| [reply] [d/l] [select] |
|
| [reply] |
Re: construct a standard object oriented program
by darksym (Beadle) on Mar 30, 2002 at 07:50 UTC
|
Using sub new as a constructor isn't mandatory although it
may make sense for purposes of uniformity. sub new {} could
just as easily be sub creatething {} as described in the perl
manual pages: perlobj, perlmod, perltoot. Recommended reading.
If you want even more info, pickup a copy of the book "Programming
Perl" (the irreverent Camel Book). This is a good starting point.
Also, take a look at other's modules. Learning by example. | [reply] |
Re: construct a standard object oriented program
by Anonymous Monk on Mar 30, 2002 at 12:53 UTC
|
... and since you want to build a class (or module, in Perl-ish), you definitely must start with h2xs. This will create automatically, the skeleton of your module and is a really time saver.
Regards,
| [reply] |
|
| [reply] |
|
h2xs -A -X FooBar
And look inside the newly created directory (FooBar)
for a pleasant surprise. ;)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
| [reply] [d/l] |
Re: construct a standard object oriented program
by Anonymous Monk on Mar 30, 2002 at 22:41 UTC
|
It really doesn't matter what you call it. I tend to give it a name in context with the name of the module. For instance, I am writing a wrapper around DBI.pm to handle all my database calls and I call the instantiating subroutine connect.
What is important is that you have a sub that blesses a hash. Objects are really hash references and blessing allows them to call subs from their module as well as the information stored within them.
To be specific, you need:
#...
my $self = bless {};
#...
return $self;
#
Note that 'return $self' is not required if bless is on the last line of your code and you don't assign the bless to a scalar.
Hope that helps!
| [reply] |
|
What is important is that you have a sub that blesses a hash. Objects are really hash references ...
Objects need not be hash references. Rather, any reference whose referent has been bless()ed into a particular class (read: package). The reference may be an array reference, scalar reference or even a reference to a subroutine.
/prakash
| [reply] |
Re: construct a standard object oriented program
by Anonymous Monk on Mar 31, 2002 at 11:56 UTC
|
I wrote a simple OO tutorial a while ago, perhaps it can help you.
look for it on http://japh.nu/index.cgi?base=tuts
hth,
-kane | [reply] |
|
oh sorry i can't go there~
| [reply] |