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

Hoping someone can just give me some guidance, so I can safely unload the gun before shooting myself in the foot. ;)

I have a module that saves some data to a site-specific directory; the module will be used by several sites hosted on the same physical server. Therefore I have a method that sets the directory; but I made it a class method, so that I could set it once like

use MyModule; MyModule->save_dir('/siteX/some/dir');

and then initialize a bunch of instances of the MyModule object.

Then a sudden fear strikes me - will a script using MyModule running at siteX and setting the directory accordingly, interfere with the same script that will be called from siteY (and therefore setting the directory property to another directory)? What I mean is, will it be safe for two scripts identical except running on different virtual servers and setting a different directory variable, interfere with each other? My thought was that I'd be OK but like I said, a sudden panic attack struck me ;)

Here are the class data methods:

# class data and class method accessors { my $ERROR = ''; my $POST_DIR = ''; sub error { my $self = shift; my $class = ref($self) || $self; # get class of object +if called as instance method if (@_) { $ERROR = $class . ' error: ' . shift; } return $ERROR; } sub post_dir { my $self = shift; $POST_DIR = shift if @_; return $POST_DIR; } }

I hope what I'm asking is clear; let me know if not.
...Guv

Replies are listed 'Best First'.
Re: OO module question re: class data vs. instance data
by japhy (Canon) on Feb 06, 2002 at 02:45 UTC
    You're safe.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      OK thanks japhy for restoring my sanity. Hopefully this is one of those things you look back on as a real learning experience!!

      ++ for all who gave their comments!

      ...Guv

Re: OO module question re: class data vs. instance data
by mstone (Deacon) on Feb 06, 2002 at 22:47 UTC

    If you still feel like being paranoid, you can move the directory back into the object, and create a trivial Factory class that generates objects configured to a preset value. It gives you the same level of convenience, and factors the work of configuring the module into an explicit class:

    package Module; sub new { return (bless {}, shift); } sub use_directory { my $O = shift; $O->{'directory'} = shift; return; } sub jabber { my $O = shift; return ("dir = $O->{'directory'}"); } package Factory; sub new { return (bless {}, shift); } sub use_directory { my $O = shift; $O->{'directory'} = shift; return; } sub mk_module { my $O = shift; my $M = new Module; $M->use_directory ($O->{'directory'}); return ($M); } package main; $f1 = new Factory; $f1->use_directory ('/usr/local'); $f2 = new Factory; $f2->use_directory ('/var/log'); for (1..10) { $f = (rand() < .5) ? $f1 : $f2; push @modules, $f->mk_module; } for $m (@modules) { print $m->jabber, "\n"; }
Re: OO module question re: class data vs. instance data
by Fastolfe (Vicar) on Feb 06, 2002 at 22:15 UTC
    Class data like that is only persistent for the lifetime of the process that uses that module. So unless you are sharing Perl interpreter processes between sites (which might be in the case of a mod_perl installation), you have nothing to worry about.