Module::DynamicSubModule - Call new modules like calling subroutines!
package Sample; @ISA = qw|Module::DynamicSubModule|; use Module::DynamicSubModule; sub new(@){ shift; return Module::DynamicSubModule->new('Sample',shift,@_); } # There are Sample, Sample::Module1 installed. use Sample; $base = Sample->new; # makes Sample instance. $sub = Sample->new('Module1'); # makes Sample::Module1 instance. $sub = Sample->Module1; # makes Sample::Module1 instance.
Usually, if you want to use module A::B, you first need to import A::B. And usually, instance of A::B will be created by below.
use A::B; $instance = A::B->new;
By using Module::DynamicSubModule, you only need to import A. If you want to create instance of A::B, you only need to type either shown below.
use A; $instance = A->new('B'); $instance = A->B;
The name of submodule can be given as a first parameter of subroutine new, or as a subroutine name.
Hacks used in this module are very tricky, and also a little bit fragile. The rules written below will may help you decrease complicated bugs. These are recommended.
If A::B was not installed, but was called, it warns about it. Next, it looks up subroutines defined in A, named B. If it exists, it will be called with srguments. Else, nothing will happen. Subroutine new defined in A will return an undefined value, so some "subroutine called on an undefined value" errors may occur.
Unfortuneately, A::B->new will be called. This problem will never occur if you follow the rules.
Even if you follow the rules, still some warnings may occur with any errors. The reason is easy, all parameters for subroutine new should have even numbers of arguments(if you follow the rules), but if the caller only wanted to call the subroutine WITHOUT any subroutines, the first given argument will be used to try as an submodule name. As a result, this logical error makes bunches of errors. This may make some people frustrated.
So, I made a boolean flag to dispose all the errors. Use it like this.
$Module::DynamicSubModule::isDebugMode = 0;
By using this, all the errors will be disposed. So, all the stupid errors will never come up to you again.
But don't forget, the errors occured in submodules will also be disposed. Even compile errors. I will recommend to stand this flag while developing.
Digest
Izumi Kawashima, <xor@>
Copyright (C) 2007 by Izumi Kawashima
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
1 POD Error
The following errors were encountered while parsing the POD:
=back doesn't take any parameters, but you said =back 4
To install Module::DynamicSubModule, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::DynamicSubModule
CPAN shell
perl -MCPAN -e shell install Module::DynamicSubModule
For more information on module installation, please visit the detailed CPAN module installation guide.