package MyClass; use warnings; use strict; use diagnostics; # If the first parameter is already an object of this # class, simply return it, otherwise instanciate the # new class and return the object. # Upon error, returns undef. sub _new_or_old { my $invokant = shift; if( defined( $invokant ) ) { if( ref( $invokant ) eq 'MyClass' ) { return $invokant; } elsif( !ref( $invokant ) && ($invokant eq 'MyClass') ) { my $self = {}; bless( $self, $invokant ); return $self; } } # If not defined $invokant, we *could* use a default. # As of now, we treat it as any other error. return undef; } #### my $self = MyClass::_new_or_old( shift ); #### sub init_stuff { my $self = MyClass::_new_or_old( shift ); # Set params here return $self; } sub start_stuff { my $self = MyClass::_new_or_old( shift ); $self->{'start_called'}++; # Do lots of really neat stuff here. return $self; } sub use_stuff { my $self = MyClass::_new_or_old( shift ); if( !$self->{'start_called'} ) { $self->start_stuff(); } return $self; } 1; #### my $self = shift; if( !$self->{'constructor'}++ ) { $self = Myclass::new(); }