9.6. Making a Method Work with Either Classes or InstancesAll you need to fix this is a way to detect whether the method is called on a class or an instance. The most straightforward way to find out is with the ref operator. This operator returns a string (the classname) when used on a blessed reference, and undef when used on a string (like a classname). Modify the name method first to notice the change:
Here the ?: operator selects either the dereference or a derived string. Now you can use it with either an instance or a class. Note that you changed the first parameter holder to $either to show that it is intentional:
and now you'll fix speak to use this:
Since sound already worked with either a class or an instance, you're done!
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|