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 coded a really simple test class that would handle the "DEFINITION" subroutine attribute and would just print whatever it got. Just for testing.
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:
use Class::Definition; sub TEST : DEFINITION('whatever') {}
I succesfully get this printed: UNIVERSAL::DEFINITION: main GLOB(0x283179c) CODE(0x225454) DEFINITION whatever CHECK

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:
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 );
When it runs I get this error message:
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:
use warnings; use strict; use Class::Definition; sub CLASS : DEFINITION( ATTRIBUTES: );
I get this as an error:
Invalid CODE attribute: INITION( ATTRIBUTES: ) at attrtest.pl line 6 BEGIN failed--compilation aborted at attrtest.pl line 9.
As you can see, the attribute name now got changed to INITION. It seems to be overwriting itself somehow.

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


In reply to Abusing attributes (possible perl bug?) by acid06

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.