in reply to Any suggestions on cleaning up a module for submission to CPAN?

I prefer pluggable logging objects personally.

Here's an untested example of what I mean:

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(@_); }
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:
my $logger = SomeOtherLogger->new(log_level => 'debug'); my $foo = Foo->new(LOGGER => $logger);
As long as SomeOtherLogger uses the same interface you're ok. To make it more robust you can do something like this:
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(@_); }

Update - switched to require within the eval, per chromatic's suggestion

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

    Do you mean instead:

    require SomeLogger;

    ... in the eval block? I'm not sure whether you intended a conditional load.

      Aye, that was my intent.
      eval { require SomeLogger; $self->{LOGGER} = SomeLogger->new(); };