Destructor
- destructor
- DESTROY
use strict;
use warnings;
use v5.10;
use Person;
my $first = Person->new( name => 'Bar' );
say $first->name; # Bar
{
my $second = Person->new( name => 'Foo' );
say $second->name; # Foo
} # Foo is dying
# Bar is dying
package Person;
use strict;
use warnings;
sub new {
my ($class, %args) = @_;
my $self = \%args;
bless $self, $class;
return $self;
}
sub name {
my ($self, $value) = @_;
return $self->{name};
}
DESTROY {
my ($self) = @_;
print $self->name, " is dying\n";
}
1;