Don't ask to ask, just ask | |
PerlMonks |
Re^3: How to tie a hash to a classby afoken (Chancellor) |
on Sep 08, 2018 at 13:54 UTC ( [id://1221949]=note: print w/replies, xml ) | Need Help?? |
Please edit your posting to have a <code> block per file, and another <code> block for the result. Did you read tie? You really should, because it specifies how to write a class usable for tie(). Tieing a hash (or array, scalar, handle, ...) to a class completely hides the original hash (array, ...) and replaces it with the tied one. Now look at your example:
tie calls TIEHASH() for you, passing all of its arguments, except for the first one, to TIEHASH(). The first argument to TIEHASH() is the class name, that your code correctly assigns to $class. The remaining ones form the content of your new hash. Now look at the debug output from your code:
When TIEHASH() is called, there is only one argument: The class name. So your tied hash is empty. If you use Data::Dumper, you can see that even better:
Ignore the "Can't locate object method" error for a while, your first problem is in the tie call. I've already explained what goes wrong, and fixing it is easy. (RTFM!) The "Can't locate object method" error tells you that your tahash class needs a few more methods. Again, RTFM. Update: If you are lazy (and that's one of the virtues of any programmer), you can use existing code to implement the missing methods. See Tie::Hash:
Output:
Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
In Section
Seekers of Perl Wisdom
|
|