in reply to Sharing a log file across classes...

Use a singleton factory. Create a class that instantiates a a logger once. Then, every call to this factory will return the same instance. Something along the lines of
package Factory::Singleton::Log; my $LOGGER = Log::Common->new(SOME PARAMETERS HERE); sub new { my $class = shift; return undef if ref $class; return $LOGGER; } 1; __END__

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: Sharing a log file across classes...
by Anonymous Monk on Feb 27, 2002 at 21:00 UTC
    I'm not sure it needs to even be that complex. If you are using all three classes in the same script, why not simply pass the log object in the constructor?
    my $log = Log::Common->new; my $foo = Foo::Foo->new('log' => $log); my $bar = Foo::Bar->new('log' => $log); my $baz = Foo::Baz->new('log' => $log);
    I have used a similar method for sharing database handles amongst several different classes.
      That is definitely a perfectly good way of doing things. However, I like to encapsulate things. The client of that class hierarchy doesn't need to know what logger is being used, so allow for some default behavior. If the client wishes to override that behavior, then allow for a logger to be set, both in the constructor and by some 'setter'.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        In that case, I think the singleton is definately the best approach. That allows you to create a class which will return the same instance to everything that calls its constructor.