My favorite answer would be to encapsulate this into some sort of Factory, pass the factory the value, and have it create the instance of the correct class.
Of course, this is just punting the answer off to some factory class. How do you do it in the factory??
Another, more extensible, possibility would be to do something like:if ($value eq 'A') { eval "use Blah::First"; } elsif ($value eq 'B') { eval "use Blah::Second"; } else { eval "use Blah::Default"; }
Now, require doesn't do an import() the way use does. Thus, if you have an import() anywhere in your hierarchy (typically if you use Exporter), then you have to make that function call yourself, maybe like "Blah::$value"->import(). That should work, but it is untested.my $basedir = "/some/base/dir/for/perl/Blah"; require "$basedir/$value"; # or --- eval "use Blah::$value";
I would recommend the first alternative, unless you feel really wild and free. The second is a little less secure and you have less control over it.
Update: Changed to use eval, as per merlyn's comment.
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
In reply to Re: Inheritance and Container Classes and use/require
by dragonchild
in thread Inheritance and Container Classes and use/require
by TGI
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |