Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re^4: "Fields" for "Objects"by stvn (Monsignor) |
on Jun 12, 2009 at 00:39 UTC ( [id://770797]=note: print w/replies, xml ) | Need Help?? |
Each subsequent call to the method accesses the sub normally, i.e. through typeglob lookup. Yes, this is true, however this is going deeper down the slippery slope where the amount of complex code to maintain is far outweighing the benefits of saving some typing. Take for instance, stack traces. Because you assigned an anon sub ref into a typeglob any stack traces will see the name of this method (and any other ones you created with AUTOLOAD) as simple __ANON__, which means you have no way of distinguishing them from one another. While this might not seem like much of an issue to some, it is a small details that would really be helpful when your up against the wall trying to fix a bug in the 11th hour. Of course, this too is solveable, Ovid had a hack where he assigned a name to the __ANON__ typeglob slot using local I don't recall the details, and there is also Sub::Name on the CPAN. But either way your now depending on an XS module (Sub::Name) or doing some weird and nasty local/typeglob hackery. That plumbing amounts to writing a proper import() subroutine which calls AUTOLOAD for the inherited fields and exports them to the inheriting class Yes, but it wouldn't be that simple if you add in multiple inheritance and the possibility of conflicts/diamond inheritance. Also I am not sure how this would work for deeply inherited fields (more then one level away), but perhaps I am just not understanding the solution fully. Again, all might be solvable problems, but now you've gone from "saving myself some tedious typing" to "building my own object system with AUTOLOAD". But then, you're right, why reinvent the wheel (there are reasons, though) if there are plenty modules out there which handle that. The single best reason to re-invent the wheel is to do it as a learning tool. I highly recommend the practice, especially if you have other well written versions to learn/steal from. Moose would not exist if it were not for CLOS and more specifically the Art of the MetaObject Protocol book.
-stvn
In Section
Seekers of Perl Wisdom
|
|