Doing something "before the first call to add_column" could be accomplished by adding your first time code to add_column, protected by a flag.

my $_added_columns = 0; sub add_column { my $self = shift; if ( ! $_added_columns++ ) { $self->_initialize_class(); } }

Update: Instead of testing some variable that's not used for anything else, you could test the data structure you use to store column information. That will work fine until someone does an add, remove, add sequence—if that's even possible.

Detecting the "last call to add_column" is different. If you know that all your add_column calls are done before your first instantiation, you could do your initialization when that happens.

my $_objects_created = 0; sub new { my $self = shift; if ( ! $_objects_created++ ) { $self->build_class(); } # construction continues }

This is all kind of a clutter, though. There are stray private variables. There's an overhead to every new call as it checks to see if it's been called before. It would probably be a lot neater, if possible, to add it to where you add columns already.

__PACKAGE__->initialize_class(); __PACKAGE__->add_column('name'); __PACKAGE__->add_column('address'); __PACKAGE__->build_class();

In reply to Re: Treating classes as objects by kyle
in thread Treating classes as objects by roman

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.