http://www.perlmonks.org?node_id=695618

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

So I have a suite of modules under a namespace. Lets call it 'Foo::Bar'. So one of these modules would be:
Foo::Bar::Asdf.pm
Anyway, I noticed that all of the modules under 'Foo::Bar' use CGI::Carp 'fatalsToBrowser'.
package Foo::Bar::Asdf; use CGI::Carp 'fatalsToBrowser';
There are a lot of modules under this namespace and they all import the same subroutine. What I would like to do is to stop declaring use CGI::Carp 'fatalsToBrowser' for every module and only declare it once. How could I do this, without using a new base class?

Replies are listed 'Best First'.
Re: Automatically importing a subroutine for a particular namespace
by Joost (Canon) on Jul 04, 2008 at 18:56 UTC
    AFAIK fatalsToBrowser is not an exported subroutine, but a hook that installs a process-wide DIE handler. IOW you generally only want to call fatalsToBrowser once, at some place that easy to find so you can remove it when deploying your code in production.

    In the general case of importing modules/variables in lots of modules, I prefer to import everything I want into some package and automatically export them from there:

    package My::Tools; use Something qw(foo bar); use Other qw(blip blob); use Exporter; our @ISA = 'Exporter'; our @EXPORT = qw(foo bar blip blob);
    Note that this package does not have to be a base class of anything. You only have to use it wherever it's needed:

    package Foo::Bar::Asdf; use My::Tools;

      Note that this package does not have to be a base class of anything. You only have to use it wherever it's needed

      That's what I am trying to get away from... How can I tell perl to use something under a particular package without declaring it under that package? (I want to be lazy ...)

      Basically, I want to tell perl: "Hey, see that package name? For any package name you find like that, go ahead and make use of this...".

Re: Automatically importing a subroutine for a particular namespace
by pc88mxer (Vicar) on Jul 04, 2008 at 18:51 UTC
    AFAIK, only one module needs to use CGI::Carp qw(fatalsToBrowser). It's not a typical function import as it modifies global behavior of warn and die.

    Remove those use statement from your modules, and make it the responsibility of the program using your modules to import fatalsToBrowser.

Re: Automatically importing a subroutine for a particular namespace
by dragonchild (Archbishop) on Jul 05, 2008 at 04:39 UTC
    It sounds like you have inherited a lot of cargo-cult code. I would wrap a test suite around that puppy ASAP. I would also look at removing as many of the design patterns from it as possible. In my experience, most design patterns aren't needed, either when they were implemented or, especially, now.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?