Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Abusing attributes (possible perl bug?)

by acid06 (Friar)
on Feb 08, 2006 at 03:05 UTC ( [id://528699]=perlquestion: print w/replies, xml ) Need Help??

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 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

Replies are listed 'Best First'.
Re: Abusing attributes (possible perl bug?)
by chromatic (Archbishop) on Feb 08, 2006 at 07:15 UTC

    It's late here and I should probably sleep, test my idea in the morning, and only then answer, but I've always had trouble with attribute data that spans lines. In my tests it only worked correctly without newlines, as there seemed to be some sort of parser problem. I didn't trace it back to the source though.

Re: Abusing attributes (possible perl bug?)
by salva (Canon) on Feb 08, 2006 at 09:51 UTC
    I don't know if it is a bug or "by design" but you can not split attributes over several lines.
      Well, then I guess it's a bug since then it should probably emit a parser error.

      But note that this works:
      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 ;

      code edited: missing ':'
      It gets interpreted to a lot of attributes. And it spans multiple lines.

      Just a side note, but I actually wrote code that handles this sort of attribute depiction and creates the accessors methods and such.

      Somehow, yesterday this all seemed like a very good idea but today it seems like a brain-dead way to do things.
      But I'm still curious about all of this. Should I report it as a bug or something like that?


      acid06
      perl -e "print pack('h*', 16369646), scalar reverse $="
        the bug seems to affect only expressions where opening {, [ and { do not match clossing }, ] and ) on every line.

        and yes, you should report it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://528699]
Approved by spiritway
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found