Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

References

 A reference is a pointer that refers to a Perl variable, an array, a


hash or even a subroutine.
 A reference is simply an address to a value.
 Using the backslash(\) operator, is similar to using & in ‘C’
language.
 We use \ to create a new reference to a variable.
 Creating references in Perl is very easy.
References and scalars
References and Arrays

References and Hashes


References and subroutines
Object Oriented Programming
 A module is a Perl Package.
 They are also called as “Classes”.
 We can create a class and then create objects for the class.
 A method is a subroutine defined inside a class. The only
difference between a method and a subroutine is that a
method should have the class name as its first argument.
 An object is a reference to some data item in the class.
Classes in Perl
 Each class is a package in Perl.
 They are created with the extension “.pm” i.e Perl Module.
 Classes in Perl are created using the keyword “package”.

Constructors and blessing constructors


 A constructor is a method in a class that will be called
automatically whenever an instance/object is created for the
class.
 Constructors of a class will have the name “new”.
 Connecting an object with the class name is called “blessing the
object”.
Ex:
bless reference, [classname]
 Here ‘classname’ is optional. When it is omitted, the class name
will be deduced from the object.
Reference variables

 The arguments to new() function for a constructor are called


“reference variables”.
 They are used to do initialization for each instance of an object
as it’s created.
Ex:
package person;
#constructor
sub new
{
my $this=[];
my @arr=@_;
shift @arr;
$this->[0]=$arr[0];
$this->[1]=$arr[1];
print "ht=$this->[0]\t";
print "wt=$this->[1]\n";
}
1;

main file
use person;
$p1=new person(5.2,50);
methods
 A method is a subroutine defined inside a class.
 A method expects its first argument to be the name of the
class.
Inheritance
Method Overriding
Multiple inheritance

You might also like