in reply to OOP Question - location of $self

It's bad.

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;"

  • Comment on (crazyinsomniac) Re: OOP Question - location of $self

Replies are listed 'Best First'.
Re (tilly) 2: OOP Question - location of $self
by tilly (Archbishop) on Apr 16, 2001 at 07:19 UTC
    Actually he has $self as a lexical variable whose scope happens to be fairly close to the scope of the package, but it isn't a global.

    Otherwise what you say is pretty much correct. (Also see my anon explanation.)

    UPDATE
    Before saying that the difference is minute, see my feedback to your Morse module. The difference is fundamental and appears to be the source of some serious misunderstandings on your part.