Clear questions and runnable code
get the best and fastest answer
Re: Class::Std : How does MODIFY_HASH_ATTRIBUTES work?by xdg (Monsignor)
|on Oct 13, 2005 at 21:24 UTC||Need Help??|
It's part of the attributes mechanism. It's automatically called to handle the attributes of an appropriate type:
You might also want to browse the source of Attribute::Handlers for examples of how they get used.
Updated: expanded answer...
attributes in turn, looks for a MODIFY_*_ATTRIBUTES in the current package or the @ISA array and calls it to handle the attribute. If that function returns any attributes, those are "unhandled" attributes and the import croaks with an error.
MODIFY_*_ATTRIBUTES receives the package name, a reference to the item attributed, and a list of attributes attached. E.g. in Class::Std:
The rest of that routine checks for an "ATTR" attribute, extracts any information passed along with it, like defaults, and installs the accessors as "usual". For example:
The only "magic" here is the way that Perl automatically expands the ":ATTR" into a call to the attributes module. After that, it's just normal function calls between modules.
CAVEAT: this all happens at compile time -- so it's like a BEGIN block and certain things aren't yet set up. This is why in MODIFY_CODE_ATTRIBUTES, the name and reference of the method are just pushed onto a data structure, and the actual handling of the attribute occurs later in the CHECK block, after the method has been compiled.
(This is why anything build with Class::Std won't work with mod_perl, which has no CHECK phase. You can see an approach which will work in my own experimental Object::LocalVars, which requires the module using it to make an explicit function call to give_methods which gets run after its compilation when the module is first loaded in what is effectively a BEGIN phase at that point.)
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.