acid06 has asked for the wisdom of the Perl Monks concerning the following question:
I'm planning on writing yet another module which handles/enforces class interface contracts (e.g. public/private method and the like). My motivation is that none of the currently existing ones handle everything I want with a really simple interface. Class::Meta came close but I really think its interface is somewhat verbose.
Anyway, I came up with the idea of using subroutine attributes for this interface. Now, I know some of you might be screaming "that's the weirdest idea ever". But just forget the concept for now and focus on the implementation, since that's the point of this question.
I find this all too weird. Any help or advice would be appreciated.
acid06
perl -e "print pack('h*', 16369646), scalar reverse $="
Anyway, I came up with the idea of using subroutine attributes for this interface. Now, I know some of you might be screaming "that's the weirdest idea ever". But just forget the concept for now and focus on the implementation, since that's the point of this question.
I coded a really simple test class that would handle the "DEFINITION" subroutine attribute and would just print whatever it got. Just for testing.
That's all sweet and dandy and if I say:
Now comes the tricky part.
I think the code below should work. But it doesn't. I don't know if there's an obvious mistake and I'll be ashamed for eternity for ever writing this node. But I truely believe it's some kind of perl bug, but I hope to be wrong. Here's the code:
Do you see something wrong there? The attribute name got changed to pub_mt1, which is a substring of the attribute parameter.
But wait, there's even more weirdness. If I strip some lines off the code, so that it ends up like this:
As you can see, the attribute name now got changed to INITION. It seems to be overwriting itself somehow.package Class::Definition; use Attribute::Handlers; sub UNIVERSAL::DEFINITION : ATTR(CODE) { print "UNIVERSAL::DEFINITION: @_\n"; } 1;
That's all sweet and dandy and if I say:
I succesfully get this printed: UNIVERSAL::DEFINITION: main GLOB(0x283179c) CODE(0x225454) DEFINITION whatever CHECKuse Class::Definition; sub TEST : DEFINITION('whatever') {}
Now comes the tricky part.
I think the code below should work. But it doesn't. I don't know if there's an obvious mistake and I'll be ashamed for eternity for ever writing this node. But I truely believe it's some kind of perl bug, but I hope to be wrong. Here's the code:
When it runs I get this error message:use warnings; use strict; use Class::Definition; sub CLASS : DEFINITION( ATTRIBUTES: PRIVATE: pvt_at1 pvt_at2 pvt_at3 PROTECTED: ptc_at1 ptc_at2 ptc_at3 PUBLIC: pub_at1 pub_at2 pub_at3 METHODS: PRIVATE: pvt_mt1 pvt_mt2 pvt_mt3 PROTECTED: ptc_mt1 ptc_mt2 ptc_mt3 PUBLIC: pub_mt1 pub_mt2 pub_mt3 );
Invalid CODE attribute: pub_mt1 ( ATTRIBUTES: PRIVATE: pvt_at1 pvt_at2 pvt_at3 PROTECTED: ptc_at1 ptc_at2 ptc_at3 PUBLIC: pub_at1 pub_at2 pub_at3 METHODS: PRIVATE: pvt_mt1 pvt_mt2 pvt_mt3 PROTECTED: ptc_mt1 ptc_mt2 ptc_mt3 PUBLIC: pub_mt1 pub_mt2 pub_mt3 ) at attrtest.pl line 6 BEGIN failed--compilation aborted at attrtest.pl line 18.
Do you see something wrong there? The attribute name got changed to pub_mt1, which is a substring of the attribute parameter.
But wait, there's even more weirdness. If I strip some lines off the code, so that it ends up like this:
I get this as an error:use warnings; use strict; use Class::Definition; sub CLASS : DEFINITION( ATTRIBUTES: );
Invalid CODE attribute: INITION( ATTRIBUTES: ) at attrtest.pl line 6 BEGIN failed--compilation aborted at attrtest.pl line 9.
I find this all too weird. Any help or advice would be appreciated.
acid06
perl -e "print pack('h*', 16369646), scalar reverse $="
Edited by planetscape - added readmore tags
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Abusing attributes (possible perl bug?)
by chromatic (Archbishop) on Feb 08, 2006 at 07:15 UTC | |
Re: Abusing attributes (possible perl bug?)
by salva (Canon) on Feb 08, 2006 at 09:51 UTC | |
by acid06 (Friar) on Feb 08, 2006 at 14:04 UTC | |
by salva (Canon) on Feb 08, 2006 at 14:31 UTC | |
by TheDamian (Vicar) on Feb 08, 2006 at 20:18 UTC |
Back to
Seekers of Perl Wisdom