Perl Tutorial - Practical Extraction and Reporting Language (Perl)

Please leave a remark at the bottom of each page with your useful suggestion.


Table of Contents

  1. Perl Introduction
  2. Perl Program Startup
  3. Perl Regular Expressions
  4. Perl Array Program
  5. Perl Basic Program
  6. Perl Subroutine / Function Program
  7. Perl XML Program
  8. Perl String Program
  9. Perl Statement Program
  10. Perl Network Program
  11. Perl Hash Program
  12. Perl File Handling Program
  13. Perl Data Type Program
  14. Perl Database Program
  15. Perl Class Program
  16. Perl CGI Program
  17. Perl GUI Program
  18. Perl Report Program

Perl Class Program


A class is really just a package

 #!/bin/perl
package main;
$name = "Tom";
my $birthyear = 1942;
package nosy;
print "$main::name.\n";
print "$main::birthyear?\n";

Adding A Class Attribute

 package Person;
use warnings;
use strict;
use Carp;
my $Population = 0;
sub new {
    my $class = shift;
    my $self = {@_};
    bless($self, $class);
    $Population++;
    return $self;
}
# Object accessor methods
sub address { $_[0]->{address }=$_[1] if defined $_[1]; $_[0]->{address } }
sub surname { $_[0]->{surname }=$_[1] if defined $_[1]; $_[0]->{surname } }
sub forename { $_[0]->{forename}=$_[1] if defined $_[1]; $_[0]->{forename} }
sub phone_no { $_[0]->{phone_no}=$_[1] if defined $_[1]; $_[0]->{phone_no} }
sub occupation {
$_[0]->{occupation}=$_[1] if defined $_[1]; $_[0]->{occupation}
}
# Class accessor methods
sub headcount { $Population }
1;
#!/usr/bin/perl
use warnings;
use strict;
use Person;
print "In the beginning: ", Person->headcount, "\n";
my $object = Person->new (
    surname => "G",
    forename => "G",
    address => "Apts.",
    occupation => "tester"
);
print "Population now: ", Person->headcount, "\n";

A Perl class is a package containing a collection of variables and functions, called properties and methods.

 There is no "class" keyword. 
The properties are variables used to describe the object. 
Methods are functions that create and manipulate the object. 
Objects are created with the bless function.
#Creating a Class
package Pet
sub new{ # Constructor
    my $class = shift;
    my $pet = {
        "Name"  => undef,
        "Owner" => undef,
        "Type"  => undef,
    };
    bless($pet, $class);
    sub set_pet{    
        my $self = shift;
        my ($name, $owner, $type)= @_;
        $self->{'Name'} = $name;
        $self->{'Owner'}= $owner;
        $self->{'Type'}= $type;
    }
    sub get_pet{
    my $self = shift;
    while(($key,$value)=each($%self)){
        print "$key: $value\n";
    }
}
#Instantiating a Class
$cat = Pet->new(); 
# Create an object with a constructor method
$cat->set_pet("Sneaky", "Mr. Jones", "Siamese");
# Access the object with an instance
$cat->get_pet;

Cat class and dog class

 package Cat;
sub new{
   my $class=shift;
   my $dptr={};
   bless($dptr, $class);
}
sub set_attributes{
   my $self= shift;
   $self->{"Name"}="Sylvester";
   $self->{"Owner"}="Mrs. Black";
   $self->{"Type"}="Siamese";
   $self->{"Sex"}="Male";
}
sub get_attributes{
   my $self = shift;
   while(($key,$value)=each( %$self)){
      print "$key is $value. \n";
   }
1;
# Dog.pm
package Dog;
sub new{             
    my $class=shift;
    my $dptr={};
    bless($dptr, $class);
}
sub set_attributes{
    my $self= shift;
    my($name, $owner, $breed)=@_;
    $self->{"Name"}="$name";
    $self->{"Owner"}="$owner";
    $self->{"Breed"}="$breed";
}
sub get_attributes{
    my $self = shift;
    print "All about $self->{Name}\n";
    while(($key,$value)= each( %$self)){
       print "$key is $value.\n";
    }
}
1;
#main.pl
#!/bin/perl
use Cat;
use Dog;
my $dogref = Dog->new;     
my $catref= Cat->new;
$dogref->set_attributes("Tom", "Jack", "Mutt");
$catref->set_attributes;   
$dogref->get_attributes;
$catref->get_attributes;

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";

How to create an object

 #First we create an anonymous hash, 
#then bless it into a package
package House;
my $ref = { "Owner"=>"Tom", 
            "Price"=>"25000", # Properties/attributes
          };
bless($ref, House);
print "\$ref is: $ref.\n";
print ref($ref), ".\n";

Initializing Attributes In The Constructor

 package Person;
use warnings;
use strict;
sub new {
    my $class = shift;
    my $self = {@_};
    bless($self, $class);
    return $self;
}
1;
#!/usr/bin/perl
use Person;
my $object = Person->new (
    surname => "G",
    forename => "G",
    address => "Apts.",
    occupation => "tester"
);
print "This person's surname: ", $object->surname, "\n";

Our First Constructor

 package Person;
use warnings;
use strict;
sub new {
    my $self = {};
    bless ($self, "Person");
    return $self;
}
1;
#Now we can use our Person class to create an object:
#/usr/bin/perl
use warnings;
use strict;
use Person;
my $person = Person->new();

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";

Passing Parameters to Instance Methods

 # Module: House.pm
#!/bin/perl
package House;
sub new{
    my $class = shift;
    my ($owner, $salary, $style) = @_;
    my $ref={ "Owner"=>$name,
              "Price"=>$salary,
              "Style"=>$style,
            };
    return bless($ref, $class);
}
sub display {           
    my $self = shift;   
    foreach $key ( @_){
       print "$key: $self->{$key}\n";
    }
}
1;
# main.pl
#!/bin/perl
use House;
my $house = House->new("A", 2, "House");
$house->display ("Owner", "Style");

The Class and Instance Methods

 # Module: House.pm
#!/usr/bin/perl
package House;
sub new{          
     my $class = shift;
     my $ref={};   
     bless($ref);
     return $ref;
}
sub set_owner{    
     my $self = shift;
     print "\$self is a class ", ref($self)," reference.\n";
     $self->{"Owner"} = shift;
}
sub display_owner {
     my $self = shift;
     print $self->{"Owner"},"\n";
}
1;
# main.pl
#!/usr/bin/perl
use House;
my $house = House->new;      # Call class method
$house->set_owner ("Tom");   
$house->display_owner;       # Call instance method

The Class Constructor Method

 # Module: House.pm
package House;                 # Class
sub new {                      #constructor
     my $class = shift;
     my $ref={"Owner"=>undef,   
              "Price" =>undef,  
             };
     bless($ref, $class);
     return $ref;    # A reference to the object is returned
}
1;
# The User of the Module)
#!/usr/bin/perl
use House;
my $houseref = House->new();
print ref($houseref),".\n";



Write Your Comments or Suggestion...