| |
| Recipe 3.7: Removing an Attribute or Method from a Class |
Last Updated: Sep 8, 2003
Status: Draft
|
|
|
|
How important is this problem to you?
(Login to Vote)
3.33 Rating, 3 Votes
|
|
How acceptable is the proposed solution?
(Login to Vote)
5.00 Rating, 1 Vote
|
|
Problem:
  
You want to remove a specific attribute or method implementation within a class.
Solution:
   
Use delete:
class MyClass { ... } # the original class
delete MyClass.foo; # eliminate the 'foo' attrib or method
Discussion:
The delete directive deletes the given attribute or method from the named class.
A novice might assume that:
MyClass.foo = undef;
accomplishes the same thing. It doesn't. Using delete MyClass.foo removes the very existence of the method foo in MyClass. The next time someone tries to call MyClass.foo, it won't exist -- at least, not that particular implementation of it. Perl will look through the parent classes to determine if any implementation of the method .foo exists, and if it doesn't, an exception willl be thrown.
The expression "Myclass.foo = undef", on the other hand, merely sets the value of foo to undef. It doesn't delete the method, and the next time someone calls the .foo method, they will receive undef as the return value. So don't do that, unless that's what you mean.
You can't delete an attribute or method if it has been declared is final, or if the class itself has been declared is final.
Issue:
Does this delete the signature and implementation of a method, or just the implementation? In other words, does delete delete all existance of a method, or does it delete the specified implementation, rendering the method abstract?
|
|