in reply to Any suggestions on cleaning up a module for submission to CPAN?
Here's an untested example of what I mean:
With this type of setup the user can run the code without any problems if the logging class is not available, and if they wish to use a different type of logger they can do something like this:package Foo; use strict; use warnings; sub new { my $class = shift; $self = bless {@_}, $class; $self->init_logging(); } sub init_logging { my $self = shift; return if exists $self->{LOGGER}; eval { require SomeLogger; $self->{LOGGER} = SomeLogger->new(); }; } sub debug { my $self = shift; return unless exists $self->{LOGGER}; $self->{LOGGER}->debug(@_); }
As long as SomeOtherLogger uses the same interface you're ok. To make it more robust you can do something like this:my $logger = SomeOtherLogger->new(log_level => 'debug'); my $foo = Foo->new(LOGGER => $logger);
use Scalar::Util qw( blessed ); sub debug { my $self = shift; return unless exists $self->{LOGGER}; return unless blessed $self->{LOGGER}; return unless $self->{LOGGER}->can('debug'); $self->{LOGGER}->debug(@_); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Any suggestions on cleaning up a module for submission to CPAN?
by chromatic (Archbishop) on Sep 01, 2006 at 01:07 UTC | |
by imp (Priest) on Sep 01, 2006 at 01:40 UTC |