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; }