in reply to OOP Question - location of $self
It makes $self a package wide variable, and if you have two objects of type YOUR::Package, $self will contain the value of the 2nd object, thus clobbering your first.
In Java's OO model, when you create a new variable of type Foo, java will create a new set of all the Class variables, except static ones, and tie them to that variable. In effect, it will create a new instance of that class.
This is not how perl works. Perl import variables into the namespace of a running program/process and does not create a new instance of global class variables, which java does do. You can achieve a similar effect by using eval (i believe), but this is not very efficient.
In Perl OO, when you have a constructor, it creates a new variable everytime it's called, where as a global $self, is only created once, upon importation of the module into the namespace.
You can implement the technique you describe if you don't use more than a single instance of an object of your package type, and anyone else using your program knows this. But, if you do that, calling your module object oriented is a funny, since you kind of suck the OO out of it.(What kind of OO is a single object OO?)
<SHAMELESS_PLUG>
In my latest post Morse::Code I implement an OO module which can optionally be set in OO less mode. You might wanna peruse the code, as I did take some time to write it, and feel I grasp the Perl OO with some authority %^)
</SHAMELESS_PLUG>
update:
ok tilly, but the difference is fairly minute.
Ok. Much appreciated.
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: OOP Question - location of $self
by tilly (Archbishop) on Apr 16, 2001 at 07:19 UTC |