| Module-New documentation | Contained in the Module-New distribution. |
Module::New::Loader
my $loader = Module::New::Loader->new('SomeClass');
my $object = $loader->load('Recipe', 'Foo', @args);
# the $object should hopefully be SomeClass::Recipe::Foo,
# or Module::New::Recipe::Foo if the former is not found.
# (or croaks if the latter is not found, either.)
This is a dedicated module loader used internally.
may take some extra namespaces, and creates a loader object.
looks for a module under the registered namespaces and loads it.
loads and creates an instance of the specified module with extra arguments.
Kenichi Ishigaki, <ishigaki at cpan.org>
Copyright (C) 2007-2009 by Kenichi Ishigaki.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Module-New documentation | Contained in the Module-New distribution. |
package Module::New::Loader; use strict; use warnings; use Carp; use String::CamelCase qw( camelize ); sub new { my ($class, @base) = @_; unless ( grep { $_ eq 'Module::New' } @base ) { push @base, 'Module::New'; } bless { _base => \@base }, $class; } sub _base { @{ shift->{_base} } } sub load_class { my ($self, @parts) = @_; @parts = map { tr/a-zA-Z0-9_://cd; camelize( $_ ); } grep { defined } @parts; foreach my $base ( $self->_base ) { my $package = join '::', $base, @parts; if ( $self->{_reload} ) { (my $file = $package) =~ s|::|/|g; delete $INC{"$file.pm"}; } local $@; eval "require $package; $package->import;"; if ( $@ ) { next if $@ =~ /^Can't locate/; croak $@; } return $package; } croak "Can't locate ".(join '::', @parts); } sub reload_class { my $self = shift; local $self->{_reload} = 1; $self->load_class(@_); } sub load { my ($self, $type, $name, @args) = @_; $self->load_class($type, $name)->new(@args); } 1; __END__