...almost there :) but you'd still have to get rid of the
"::" in case of a package name like "Foo::Module". Also,
you could write ref($self) || $self, in which case you could
call load_config on both the class or an object instance.
#!/usr/bin/perl
package Base::Module;
sub load_config {
my $class = shift;
my $module = $class->config_module;
print "about to require $module\n";
#require $module;
}
sub config_module {
$self = shift;
my $pkg = ref($self) || $self;
$pkg =~ s#::#/#g;
# optionally remove "/Module"
# $pkg =~ s#/[^/]+\z##;
return "$pkg/Config.pm";
}
package Foo::Module;
use base qw(Base::Module);
sub new {
return bless {}, shift;
}
package main;
Foo::Module->load_config();
Foo::Module->new()->load_config();
__END__
$ ./727519.pl
about to require Foo/Module/Config.pm
about to require Foo/Module/Config.pm
BTW, the sub config_module would work the same way when declared
in Foo::Module, though - if I'm understanding you correctly - your
idea was to put it in the base class... (Actually, it would first
be looked up in Foo::Module, and only then, when this fails, in Base::Module
(via inheritance).)
|