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

Continuing on from my earlier question, "use"ing from dynamic namespace, I've found myself needlessly repeating things like
use Base::Module; Base::Module::Config->import(qw( %conf ));
every time I used this code. So, naturally, I thought, I'd just re-export those from the core:
package Base::Module; use strict; use warnings; sub config_module { "Base::Module::Config" } use base 'Exporter'; BEGIN { our @EXPORT_OK = qw( &%conf ); eval 'require ' . config_module; no strict 'refs'; no warnings 'once'; *conf = \%{config_module . '::conf'}; }
This comes deceptively close to working, but I'm seeing some users of Base::Module getting one %conf and others getting a different %conf. I suspect that it's related to whether they import it using
use Base::Module qw( %conf );
or
sub base_module { "Base::Module" } BEGIN { eval 'require ' . base_module; base_module()->import(qw( %conf )); }
but I'm not positive. It could also be the distinction between being used in the final program (first form) vs. being used by other modules which are a part of this structure (second form).

In any case, what would be the correct (or at least a fully-working) way to accomplish this?

Replies are listed 'Best First'.
Re: Re-exporting dynamically-included variables
by tilly (Archbishop) on Jan 09, 2009 at 23:51 UTC
    This should work.
    package Base::Module; use strict; use warnings; use Base::Module::Config qw(%conf); use Exporter qw(import); our @EXPORT_OK = qw(%conf); # ... 1;
      That would be the obvious solution if the namespace were static, but (as explained more extensively in "use"ing from dynamic namespace) the idea here is that I want to be able to derive a Foo::Module and a Bar::Module from Base::Module which reference Foo::Module::Config and Bar::Module::Config (respectively) instead of Base::Module::Config (since the Foo and Bar applications would, naturally, each want their own configurations).
        My attitude is that that is exactly the sort of situation that eval exists for. Just be sure to check for $@ and die if you've trapped a real error.