Module::New::Loader - Module::New::Loader documentation


Module-New documentation Contained in the Module-New distribution.

Index


Code Index:

NAME

Top

Module::New::Loader

SYNOPSIS

Top

DESCRIPTION

Top

  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.)

METHODS

Top

This is a dedicated module loader used internally.

new

may take some extra namespaces, and creates a loader object.

load_class, reload_class

looks for a module under the registered namespaces and loads it.

load

loads and creates an instance of the specified module with extra arguments.

AUTHOR

Top

Kenichi Ishigaki, <ishigaki at cpan.org>

COPYRIGHT AND LICENSE

Top


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__