Looking for a base module that can automatically load sub modules
am 27.06.2007 12:16:19 von XorHi,
I am looking for a module, to make a base class module, that
automatically loads sub class modules.
Usually, to make a new Digest::MD5.pm module instance, you type
$inst = Digest::MD5->new(@args);
But, the Digest.pm module, let us type
$inst = Digest->MD5(@args);
$inst = Digest('MD5' => @args);
to do the same thing.
I want a module that can generate a module like Digest.pm.
I already made a module that can do it( let us call it
Class::AutoSubClass.pm), but there might be a similar module( but
scripted more elegantly ) in CPAN. Please tell me if there is.
The module I made( Class::AutoSubClass.pm )is below.
# Class/AutoSubClass.pm
package Class::AutoSubClass;
use strict;
use warnings;
use Carp;
sub new(@){
shift;
my $sBaseName = shift;
my $sName = shift;
my $rhDefaultDats = shift || {};
return bless $rhDefaultDats if !defined $sName or $sName eq '';
my $sClassName = $sBaseName.'::'.$sName;
eval "require $sClassName";
die if $@;
no strict 'refs';
return $sClassName->new(@_);
}
1; # end Class/AutoSubClass.pm
And you use it like this.
# Wiki/Handler.pm
package Wiki::Handler;
use strict;
use warnings;
use Carp;
use Class::AutoSubClass;
sub new(@){
shift;
return Class::AutoSubClass->new('Wiki::Handler',shift,@_);
}
1; # end Wiki/Handler.pm
By using this module, the new Wiki::Handler::HTMLFragment.pm object
can be generated like
$htmlf = Wiki::Handler->new('HTMLFragment');
Thanks.