leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I have something I'm a little confused about.. Class data is system wide, user wide, process wide? What is the scope?

Class data lasts as long as there is a process in memory that uses the module?

Replies are listed 'Best First'.
Re: Class data scope?
by ikegami (Patriarch) on Jun 12, 2008 at 15:24 UTC

    Classes are package names into which objects are blessed.

    There's no language support for class variables, though. You can place class data in package variables or in captured lexical variables, and the data will be scoped as per those variables.

    In both cases I mentioned, the variables will last until global destruction.

Re: Class data scope?
by ikegami (Patriarch) on Jun 12, 2008 at 15:44 UTC

    Class data lasts as long as there is a process in memory that uses the module?

    There's a couple of things wrong with the question itself.

    Modules are files which are executed by use or require. It's not an ongoing process, so saying "uses" instead of "used" makes no sense.

    Memory isn't shared between Perl processes unless you explicitly use a mechanism to do that. (I don't actually know any, but if the OS provides a mechanism, a module could be used to take advantage of it.)

    And finally, class data is no different than any other kind of data. It'll last as long as the variable or object that holds it last, typically, but nothing's stopping the variable (though magic) or object from reflecting some form of persistent storage (like a database or file) or remote storage (like another process).

    What are you trying to do?

      Well, actually I'm just really trying to get it right in my head. I'm not exactly trying to do anything.

      If I have Class wide data (as oposed to Object instance data),..

      Let's say we have package House, our 'script1' instances 5 House and each House has an address, etc. Class House keeps track that there are 5 houses open.

      If two process are run of script1, does Class House register 10 House or 5 House???

      Does this change if user X and user Z both run script1 at the same time?

      I'm guessing the 'scope' of the Class data is only good for the process. Regardless of user, multiple process etc.

        You're guessing right. In Perl there is (usually) no data in common between multiple executing copies of a script. If you need to share one or more vars between different copies of a script you have to do that explicitly by mean of shared memory or other IPC methods.

        But I'm really really sure I understand what you mean with "class data". My best guess is that you mean a global var defined in the package containing the class and then yes, AFAIK any method of any object derived from that class has access to the global var which is unique for the entire script (but not unique across several executions of it)

        Careful with that hash Eugene.

        Class House keeps track that there are 5 houses open.

        Are you assuming Perl does this? No language I know does this.

        If two process are run of script1, does Class House register 10 House or 5 House???

        If the class has code to do this, it depends on that code.

        It seems to me that you're asking question based on unfounded assumptions. Classes are package names into which objects are blessed. Period. Everything else is up to you.