A few random thoughts that maybe the purveyor of your library could take note of:
You could put more bondage into that by checking ref $self eq __PACKAGE__ instead of isa, but that's the basic idea. From there, the top of each function would simply look like:my $_global; sub _self_or_create { my $self = shift; return $self if (ref $self and $self->isa(__PACKAGE__)) $_global ||= __PACKAGE__->_new(); return $_global; # assume $self eq __PACKAGE__ or something derive +d from it? }
At this point, you're back to where you started, but the user doesn't need to instantiate anything, and then won't be tempted to do silly things like create two of them.sub some_method { my $self = _self_or_create(shift); # ... }
I have a (probably irrational) fear about explicitly assigning undef. It just smells funny to me. :-){ my $obj = Vendor::Library->new(...); # code here } { my $obj = Vendor::Library->new(...); # different code here (I presume) }
In reply to Re: Is assigning undef required for DESTROY to run?
by Tanktalus
in thread Is assigning undef required for DESTROY to run?
by MidLifeXis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |