Passing Parameters to Constructor Methods : Constructor « Class « Perl






Passing Parameters to Constructor Methods

 

# Module: House.pm
package House;
sub new{         
    my $class = shift;
    my ($owner, $salary) = @_;
    my $ref={"Owner"=>$owner, 
             "Price"=>$price, 
            };
    bless($ref, $class);
    return $ref;
}
sub display_object {       
    my $self = shift;      
    while( ($key, $value)=each %$self){
       print "$key: $value \n";
    }
}
1;

# main.pl
#!/usr/bin/perl
my $house1 = House->new("A", 2);
my $house2 = House->new("B", 5);
$house1->display_object;
$house2->display_object;
print "$house1, $house2\n";

   
  








Related examples in the same category

1.Our First Constructor
2.The Class Constructor Method