hello. short version: is there an equivalent of __PACKAGE__ that will give me the name of a subclass rather than the package which immediately contains it?

er, that didn't make much sense. long version:

Class::DBI is fond of constructions like this:

package My::Actor; ... My::Actor->has_many( 'roles', 'My::Actor::Role', 'actor' );

which are normally written with __PACKAGE__ for ease of revision.

I'm consolidating a lot of shared code in a single application, which will be extendable to handle various site requirements. As part of that, I want to subclass modules like the one above, in which case I need the relationships to stick to the subclass, not the superclass.

for example, this:

package Sub::Person; use base qw(Super::Person); __PACKAGE__->has_many( 'organisations', 'Sub::Organisation', 'person' +);
package Super::Person; use base qw(Class::DBI); __PACKAGE__->table('people'); __PACKAGE__->has_many( 'sessions', 'Super::Session', 'person' ); ...

wants to define a class that inherits the session() method and database parameters from Super::Person and adds an organisations() method of its own. (has_many() defines methods that follow database relationships to return objects from the corresponding poop classes).

It works exactly as I want it to except that the has_many() defines a sessions method in Super::Person, not Sub::Person, and breaks the network of relationships. Quite reasonable of it, since __PACKAGE__ is 'Super::Person' at that point. Hence the question: how can I access the name of the current subclass while in the superclass?

But I wonder if this is even possible with class-level code like that? Any suggestions much appreciated.

thanks.


In reply to __PACKAGE__ in subclass by thpfft

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.