Tuesday, 1 April 2014

Perl + constructor + object

Perl modules:


#!/usr/bin/perl

package file15;      ---> Object Declaration

sub new                 ----> Constructor creation
{
$class=shift; 
$self={
a=>shift,
b=>shift,
};
return bless $self, $class;  --> Blessing self and class
}

sub add                        --> add function
{
$self=shift;                 
$c=shift;
$d=$self->{a} + $self->{b} + $c;
return $d;
}

1;

### perl script


use file15;

$s=file15->new(1,2);    --> Passing values to constructor & creating objects
$d=$s->add(3,4);          ---> Passing values to function
print $d;
~                                                                
~         


#####Results


6







No comments:

Post a Comment