Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Automatically importing a subroutine for a particular namespace

by jacques (Priest)
on Jul 04, 2008 at 18:38 UTC ( #695618=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Automatically importing a subroutine for a particular namespace
Select or Download Code
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 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 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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://695618]
Approved by toolic
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2014-08-31 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls