Delete *really* means delete

Matt Lawrence matt.lawrence at
Thu Feb 24 21:17:41 GMT 2011

On 24/02/2011 18:35, Simon Wistow wrote:
> sub undef_at_underscore {
>    $_[0] = undef;
> }

This will remove the caller's reference to the invocant, There's no way 
of knowing if that's the last reference, copying will keep it alive.

(my $bar = $foo)->undef_at_underscore;
# $foo is still defined.

> Other suggestions were reblessing $self into 'DEAD' and then having
> package DEAD;
> use Carp qw(confess);
> sub AUTOLOAD { confess "This object is dead" }
> 1;

This is a bit nicer, because the error message is more descriptive than 
"Can't call object method ... on an undefined value", and the effect if 
on the object itself rather than a particular reference to it.

Neither of these approaches are particularly intuitive for the caller, 
you don't normally expect objects to mutate or disappear like this. If 
that's the lesser of two evils in your situation, fair enough. As long 
as the weirdness is documented clearly that's fair warning ;-)


More information about the mailing list