Destructors and Garbage Collection : Destructor « Class « Perl






Destructors and Garbage Collection

 

#Employee.pm
package Employee;
sub new{
    my $class = shift;
    $ref={};
    bless($ref, $class);
    return $ref;
}

sub DESTROY{
    my $self = shift;
       print "Employee $self->{Name} is being destroyed.\n";
    }
1;


#!/usr/bin/perl
use Employee;
my $emp1 = Employee->new;  
           { my $emp2 = Employee->new;
             $emp2->{"Name"}="Tom";
             print "$emp2->{'Name'}\n";
           } 

my $emp3 = Employee->new;  

$emp1->{"Name"}="Dan";
$emp3->{"Name"}="Tom";

print "$emp1->{'Name'}\n";
print "$emp3->{'Name'}\n";

   
  








Related examples in the same category