http://qs321.pair.com?node_id=89188


in reply to Undoable Objects

It would also be a good idea to add a DESTROY method in that same scope so that the history gets cleaned up when objects go away:

{ my %history; sub _log { my $self = shift; my $attrib = shift; my $value = $self->{$attrib}; push @{ $history{$self} }, [ $attrib, $value ]; } sub undo { my $self = shift; return unless (scalar @{ $history{$self} }); my ($attrib, $value) = @{ pop @{ $history{$self} } }; $self->{$attrib} = $value; } sub DESTROY { my $self = shift; delete $history{$self}; } }