Day 5

Package Execution Sequence:

First the main package is executed followed by the other packages declared as per their sequence.

If you want to execute any one package before main use begin block as follows:
print "I am main package...1\n";
package Fax;
print "I am Fax package...2\n";
BEGIN{
print "I am Print package..3\n";
package Print;
}

I the above example, the Print package will get executed first followed by main and Fax.
This concept has been taken from the awk concept:

awk -F: '/root/{print}/' IP1
root:x:0:0:root:/root:/bin/bash

cat >p1.sh
BEGIN{
FS=":"
}
/root/{print}
END
{
print "Thank You!!"
}

Using a module from a different path:
If the module file is not in same directory as the PERL file .pl , push the module path to the array @INC variable which consists of the path from where the modules are loaded.
Example:
begin{
unshift(@INC,”path of the module Demo”); #or push(@INC, ”path of the module Demo”);
}
use Demo;
Demo::display();

The push operation is done in begin section to ensure that the path is imported prior to the execution of the program.

Other way to link the module is:
use lib ‘<path of the module>’;
use Demo;
Demo::display();

List of perl modules to download: http://www.cpan.org/

using shell:
cpan
cpan[1]> install DBI
cpan[1]> install Cgi
cpan[1]> install Data::Dumper
cpan[1]>install Net::Ftp

$ perldoc -l DBI #list the module path where it is installed
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/DBI.pm

cpan –l # to list all the installed modules
perldoc <module name> # help page for the modules
perldoc –m <module name>#to view the source code of the module

Multi-dimensional Data Structure:
array inside array
array inside hash
hash inside array
hash inside hash

Array of Array:
@AOA=( [r11, r12, r13],
   [r21,r22,r23],
   [r31,r32,r33]
)

#============================================================================
#Using Arrays of Arrays
#============================================================================
use Data::Dumper;

@AOA=( [r11, r12, r13],
   [r21,r22,r23],
   [r31,r32,r33]
);

print "Displaying the array data using dumper: \n";
print Dumper(@AOA);

print "Displaying array references in the array of array: \n";
print "\n@AOA\n\n";

print "Displaying data using the array dereferencing:\n";
for(@AOA)
{
                print "@$_\n";
}

print "\nDisplaying the nested array elements using nested for loop: \n";
for(@AOA)
{
                @ia=@$_;
                for(@ia)
                {
                                print "$_\n";
                }
}

print "\nDisplaying the nested array elements one by one by direct referencing: \n";
print @AOA[0]->[0],"\n";
print @AOA[0]->[1],"\n";
print @AOA[0]->[2],"\n";
print @AOA[1]->[0],"\n";
print @AOA[1]->[1],"\n";
print @AOA[1]->[2],"\n";
print @AOA[2]->[0],"\n";
print @AOA[2]->[1],"\n";
print @AOA[2]->[2],"\n";

<<output;
D:\perl\day5>perl 2.pl
Displaying the array data using dumper:
$VAR1 = [
          'r11',
          'r12',
          'r13'
        ];
$VAR2 = [
          'r21',
          'r22',
          'r23'
        ];
$VAR3 = [
          'r31',
          'r32',
          'r33'
        ];
Displaying array references in the array of array:

ARRAY(0x56db20) ARRAY(0x46cde8) ARRAY(0x46ced8)

Displaying data using the array dereferencing:
r11 r12 r13
r21 r22 r23
r31 r32 r33

Displaying the nested array elements using nested for loop:
r11
r12
r13
r21
r22
r23
r31
r32
r33

Displaying the nested array elements one by one by direct referencing:
r11
r12
r13
r21
r22
r23
r31
r32
r33
output
#============================================================================
#Referencing array of hash in PERL
#Here we will create an array containing anonymous hashes and will print the
#hash values one by one
#============================================================================
@AOH=({k1=>"v1",k2=>"v2",k3=>"v3"},
      {a1=>"b1",a2=>"b2",a3=>"b3"});

print "\nMethod1: \n";
for(@AOH)
{
                while(($k,$v)=each(%$_))
                {
                                print "key: $k and value: $v\t";
                }
                print "\n";
}

print "\nMethod2: \n";
for(@AOH)
{
                %hash=%$_;
                for(keys(%$_))
                {
                                print $hash{$_},"\t";
                               
                }
                print "\n";
}

print "\nMethod3: \n";
$i=0;
for(@AOH)
{
                for(keys(%$_))
                {
                                print $AOH[$i]->{$_},"\t";
                }
                $i++;
                print "\n";
}

print "\nMethod4: \n";
$j=0;
for(@AOH)
{
                for(keys(%$_))
                {
                                print $AOH[$j]{$_},"\t";
                }
                $j++;
                print "\n";
}


<<output;
D:\perl\day5>perl 4.pl

Method1:
key: k3 and value: v3   key: k2 and value: v2   key: k1 and value: v1
key: a1 and value: b1   key: a3 and value: b3   key: a2 and value: b2

Method2:
v3      v2      v1
b1      b3      b2

Method3:
v3      v2      v1
b1      b3      b2

Method4:
v3      v2      v1
b1      b3      b2
output
#============================================================================
#Hash inside a Hash
# This an example to display the data from a hash inside hash data structure
#============================================================================
%EMP=(Name=>{FNAME=>"Ravi",lname=>"Ranjan"},
       Dept=>{Work=>"PDIT",Team=>"EAS EM"},
       Place=>{State=>"Karnataka",City=>"Bangalore"});

print "------------------------------------------------------------------\n";
print "Method 1:\n";
print "------------------------------------------------------------------\n";
while(($x,$y)=each(%EMP))
{
                print "\n$x:\n";
                while(($x1,$y1)=each(%$y))
                {
                                print "$. $x1:$y1\n"
                }
}
print "------------------------------------------------------------------\n";
print "Method 2:\n";
print "------------------------------------------------------------------\n";
foreach $tmp_key (keys(%EMP))
{
                print "\n$tmp_key:\n";
                foreach(keys(%EMP{$tmp_key}))
                {
                                print "$EMP{$tmp_key}{$_}\t";
                }
                print "\n";
               
}


<<output;
D:\perl\day5>perl 5.pl
keys on reference is experimental at 5.pl line 26.
------------------------------------------------------------------
Method 1:
------------------------------------------------------------------

Dept:
 Work:PDIT
 Team:EAS EM

Place:
 City:Bangalore
 State:Karnataka

Name:
 lname:Ranjan
 FNAME:Ravi
------------------------------------------------------------------
Method 2:
------------------------------------------------------------------

Dept:
PDIT    EAS EM

Place:
Bangalore       Karnataka

Name:
Ranjan  Ravi

output
#============================================================================
#Hash of array
# This an example to display the data from an array inside a hash data structure
#============================================================================
%EMP=(Name=>["Ravi","Ranjan"],
       Dept=>["PDIT","EAS EM"],
       Place=>["Karnataka","Bangalore"]);

print "------------------------------------------------------------------\n";
print "Method 1:\n";
print "------------------------------------------------------------------\n";
while(($x,$y)=each(%EMP))
{
                print "\n$x:\n";
                for($y)
                {
                                print "$. @$_\n"
                }
}
print "------------------------------------------------------------------\n";
print "Method 2:\n";
print "------------------------------------------------------------------\n";
for $tmp_key (keys(%EMP))
{
                #print "$. $tmp_key $EMP{$tmp_key}->[0] \n";
                #print "$. $tmp_key $EMP{$tmp_key}->[1] \n";
                print "\n$tmp_key\n";
                for(@{$EMP{$tmp_key}})
                {
                                print "$_\t";
                }
                print "\n";
}

<<output;
D:\perl\day5>perl 6.pl
------------------------------------------------------------------
Method 1:
------------------------------------------------------------------

Name:
 Ravi Ranjan

Place:
 Karnataka Bangalore

Dept:
 PDIT EAS EM
------------------------------------------------------------------
Method 2:
------------------------------------------------------------------

Name
Ravi    Ranjan

Place
Karnataka       Bangalore

Dept
PDIT    EAS EM
output
D:\perl\day5>perl 7.pl
each on reference is experimental at 7.pl line 21.

mem2 details:
name:A  age:30
mem3 details:
name:B  age:20
mem1 details:
age:50  name:AB
$VAR1 = 'mem2';
$VAR2 = {
          'name' => 'A',
          'age' => 30
        };
$VAR3 = 'mem3';
$VAR4 = {
          'name' => 'B',
          'age' => 20
        };
$VAR5 = 'mem1';
$VAR6 = {
          'age' => 50,
          'name' => 'AB'
        };
#============================================================================

Object Oriented Concept :

In C++                
In Perl
Class
Package
Object
Reference
Method
Sub-routine
Polymorphism

Inheritance

Class box{
box();
}
Package Box;
Sub new()
{
............variable initialization..............
}
$object->me

#============================================================================
#Create a package as follows to display the OOPS style of programming:
#============================================================================
package Pack1;

sub new{
                print "@_\n";
                $class=shift(@_);
               
                $hr={code=>"123"};
               
                print "Before bless: ",ref($hr),"\n";
                bless($hr,$class);
                print "After bless: ",ref($hr),"\n";
                return($hr);
}

sub display{
print "This is the display block..\n";
print "@_\n";
}
1;
#============================================================================

#============================================================================
#Create a PERL program in OOPS style  to reference the above created package:
#============================================================================
use Pack1;
$object=Pack1->new(100,200);
print "$object\n";
$object->display('A'..'z');
<<output;
D:\perl\day5>perl 8.pl
Pack1 100 200
Before bless: HASH
After bless: Pack1
Pack1=HASH(0x4adb20)
This is the display block..
Pack1=HASH(0x4adb20) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
output

No comments:

Post a Comment