pajout has asked for the wisdom of the Perl Monks concerning the following question:
instances are of the same class, with the same methods formally, but with different subroutines (doing different things) realizing these methods.my $instance1 = MyModule::new('1.0'); print $instance1->version(); #prints '1.0'; my $instance2 = MyModule::new('2.0'); print $instance2->version(); #prints '2.0';
,blessing proper part of $verspackage MyModule; our $VERSION = '2.0'; our $vers = {'1.0'=>{__PACKAGE__.'::version'=>\&version_1_0}, '2.0'=>{__PACKAGE__.'::version'=>\&version_2_0}};
and onsub new { my $class = __PACKAGE__; my $version = shift || $VERSION; return bless({methods=>$$vers{$version}},$class); }
But i feel that AUTOLOADing is dirty for that, I wish some solution without it. Please, have you some hints or criticisms?sub AUTOLOAD { my $self = $_[0]; return &{$$self{methods}{$AUTOLOAD}}(@_); }
|
|---|