|laziness, impatience, and hubris|
my Dog $spot;by Zaxo (Archbishop)
|on Jun 06, 2006 at 08:05 UTC||Need Help??|
I was poking around in yet another odd corner of Perl, attributes.pm, and I noticed an interesting possibility. According to the pod, the statements,
should have the effect,
If all is well, &attributes::import calls Canine::MODIFY_SCALAR_ATTRIBUTES(Canine => \$spot, 'Watchful') to handle the attribute bookkeeping.
"Well," I thinks, "Don't that look just like a constructor?"
If MODIFY_SCALAR_ATTRIBUTES() is written as a constructor, then my Dog $spot; could call it on $spot and give us a dog with that purely declarative syntax. So here goes . . .
I'll simplify the example a little bit, making only one canid class,
The bark is to let us know that the constructor has been called. This is an experiment, after all, and experiments need instruments.
For a demonstration, we don't need any more methods than that. Time to test drive:
Which faithfully prints:
This is nifty all right, but it turns out to have a problem. I'd like to call the constructor with just,
Perl accepts that, but there's no "Woof!" when it runs. No Rover. Instrumenting a private copy of attributes.pm shows that &attributes::import is never called when the attribute list is empty. That's not what I expected, but I can't find any promises in the docs to say it should. In fact, that's very like the behavior of use with no import list.
The empty attribute list would be no problem for attributes.pm. Its import() function is structured to work with that.
Having that syntax available would make very happy those people who like strict typing, and I don't think it would rock the structure of Perl much. I haven't yet gone source diving to see what's needed to make that happen. Does anybody have a feel for that?