in reply to OO module question re: class data vs. instance data
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"; }
|
|---|