| |
| Recipe 4.5: Inheriting from 'Constructed' Class Names |
Last Updated: Sep 8, 2003
Status: Draft
|
|
|
|
How important is this problem to you?
(Login to Vote)
5.00 Rating, 2 Votes
|
|
How acceptable is the proposed solution?
(Login to Vote)
2.00 Rating, 1 Vote
|
|
Problem:
   
You want your class to inherit from another class, but you want to build the name of that class on the fly.
Solution:

Make sure you're forcing your constructed class name into classname context, so that is knows what to do with it:
my $parent = "ParentClass";
class MyClass is ($parent as classname) { ... }
Discussion:
Code like:
my $parent = "ParentClass";
class MyClass is $parent { ... }
will not do what you want: it instead creates a class MyClass whose parent class is the literal string "ParentClass", not a class named ParentClass.
In order to use the contents of the variable $parent as a classname, you need to make sure it's contextually interpreted as such. Thus, we use the transformation as classname.
Issue:
Alternatively, we introduce as subclass to solve the previous two recipes, making as classname unnecessary here.
|
|