in reply to "use"ing from dynamic namespace

I'd suggest using inheritance more. Either just define your load_config method in each one to do the right thing, or break it down even more with simple constant-like methods in each.

package Base::Module; sub load_config { my $class = shift; my $module = $class->config_module; require $module; } package Foo::Module; use base qw(Base::Module); sub config_module { return 'Foo/Config.pm'; }

Replies are listed 'Best First'.
Re^2: "use"ing from dynamic namespace
by dsheroh (Monsignor) on Dec 01, 2008 at 18:14 UTC
    Oddly enough, even though I was thinking of this in terms of "inheritance" and "polymorphism", it never occurred to me to take the extra step of actually turning the modules into classes. Thanks for pointing that out.
Re^2: "use"ing from dynamic namespace
by pobocks (Chaplain) on Dec 02, 2008 at 10:54 UTC

    This feels weirdly like dancing around strict refs. I know it's not.

    Would there be anything wrong with doing config_module in the base class as:

    sub config_module { return __PACKAGE__ . '::Config'; }

    Or such as that, anyway?

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      Would there be anything wrong with doing config_module in the base class as: ...

      ...the only problem is it wouldn't work ;) — at least not with perrin's code snippet.  One thing is that if you use require with a string (as opposed to a bareword), it must be a valid path fragment (i.e. / in place of ::), inlcuding the trailing .pm. The other thing is that when you declare config_module() in the base class, __PACKAGE__ would always be Base::Module, but the point of the exercise was to load different config variants (associated with Foo, Bar, etc.).

        I suspected something like that.

        I guess my next try would be:

        sub config_module { $self = shift; return ref($self) . '/Config.pm'; }
        And just always calling it as a method?

        for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";