in reply to Is assigning undef required for DESTROY to run?
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) }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is assigning undef required for DESTROY to run?
by MidLifeXis (Monsignor) on Feb 24, 2009 at 11:33 UTC |