Subject: | DBIx::Class Calls Class::C3 in a Documented Way That Will Not Work |
My Class::C3 (version 0.07) says:
## initializers
# NOTE:
# this will not run under the following
# conditions:
# - mod_perl
# - require Class::C3;
# - eval "use Class::C3"
# in all those cases, you need to call
# the initialize() function manually
INIT { initialize() }
Now, the Componentized.pm module does:
eval "package $target; import Class::C3;" unless exists $table->{$target};
...Now, whilse the C3 documentation doesn't specify that an "import"
will cause it to not run, given that "use" performs and import in a
BEGIN (among other things), I think this is why I get:
lloy0076@david:~/work/programming/pos$ perl dbix.pl
Too late to run INIT block at /usr/local/share/perl/5.8.7/Class/C3.pm
line 52.
I'm not sure of the solution here. One solution was to comemnt out line
52 of C3.pm (which struck me as silly). The other would be to work out
why the call to import is wrapped in an eval in the first place and
unwrap it.
DSL
Subject: | Componentised.pm |
package DBIx::Class::Componentised;
use Class::C3;
sub inject_base {
my ($class, $target, @to_inject) = @_;
{
no strict 'refs';
unshift(@{"${target}::ISA"}, grep { $target ne $_ && !$target->isa($_)} @to_inject);
}
my $table = { Class::C3::_dump_MRO_table };
eval "package $target; import Class::C3;" unless exists $table->{$target};
Class::C3::reinitialize() if defined $table->{$target};
}
sub load_components {
my $class = shift;
my @comp = map { "DBIx::Class::$_" } grep { $_ !~ /^#/ } @_;
$class->_load_components(@comp);
}
sub load_own_components {
my $class = shift;
my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
$class->_load_components(@comp);
}
sub _load_components {
my ($class, @comp) = @_;
foreach my $comp (@comp) {
eval "use $comp";
die $@ if $@;
}
$class->inject_base($class => @comp);
}
1;