in reply to Treating classes as objects
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();
|
|---|