package Your::Class; our $VERSION= 1.001_001; package Your::Class::_Implementation; BEGIN { require constant; my $offset= 0; for my $member ( qw< _NAME _ADDRESS _PHONE ... > ) { constant->import( $member, $offset++ ); } } sub Your::Class::new { ... $self->[_NAME]= ...; }

If you don't like writing "sub Your::Class::new" instead of "sub new", then you can instead do something like:

... package Your::CLass::_Implementation; ... sub new { ... } ... package Your::Class; BEGIN { require Exporter; for my $method ( qw< new ... > ) { Exporter::import( 'Your::Class:_Implementation', $method ); } }

This also allows you to separate your class methods fom your ojects methods, just FYI.

It isn't difficult to abstract out these tools to make them look prettier.

Note that a C' prefix is a horrible idea:

package Your::Class; ... sub C'Name() { 0; } ... package Something::Completely::Different; ... sub C'Index() { 0; } sub C'Name() { 1; } # Boom! ...

- tye        


In reply to Re: Named array indices (packages) by tye
in thread Named array indices by LanX

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.