Tuesday, June 21, 2011

perl true false

In perl  0 and "" are considered as false and all others are considered as true.

In Perl for checking the variable  is assigned , you should not check the value returned is false,as it may fail in some cases.

Consider for example:

if ($variable)
{
          print "valuse present";
}
else
{
         print "value not present"
}

This check will fail tremendously when $variable is equal to 0


The correct way to do it is


while (defined ($variable)){
          print "valuse present";
}
else
{
         print "value not present"
}

In case of hash to check if the element is present in the hash.

In this case the exists operator has to be used.


if (exists($hash{'foo'})) {      }   TRUE

In this case if you use defined function it will always return true.

Comparison in perl

Perl has two different set of comparison operators for both numbers and string.

For string we need to use only the following operators
In case of string the comparison takes place ASCIIbetically -that is by comparing ASCII values of the characters in Strings.


'a' lt 'b'         TRUE
'a' eq 'A'      FALSE-capitalization
"joseph" eq "joseph "  FALSE-spaces count
"H"  cmp "He"  -1--cmp operator

The cmp operator does a string comparison and returns -1,0,1 depending on the left argument is greater than,less than the right argument.
This operator should not be used for string comparison


"aaaaaaaa"  == "fgdfgdfgdf"    TRUE always as for equal to operator the string always looks to be 0

Numeric comparison use the following:

0<5                     TRUE
10 == 10.0          TRUE
10 <=> 9.5          1     --->spaceship operator
The spaceship operator is like cmp for strings except it compares numerically.
Should not use string comparators for numeric.

'10' gt '2'               FALSE -  '1' sorts before 2
"10.0" eq "10"       FALSE-different strings

Monday, June 20, 2011

perl array references and hash references


In case of hashes
%ddd=(ddd,ff,ff,fg.dd.ss.aa.sss,xxx,qqq,ccc);
%ddd=(ddd=>ff,ff=>fg,....);
But when it used as hash reference.
$ddd={ddd=>ff,ff=>fg,....};
To create empty hash refernce.
$ddd={};

In case of arrays it is different
@sss=("gghgh","dsdsd","qwqwqw","qqq");
When used as array refernce
$sss=["gghgh","dsdsd","qwqwqw","qqq"];
To create a empty array reference;
$sss=[];

emptying a array in perl


@array=();
undef @array;
$#aray=-1;

Here array signifies the array name

Inheritance in perl example

Kindly understand the class in Perl means Package.

In case of perl,inheritance takes place by @ISA variable.
Here the classes from which a package has to inherit can be assigned to the @ISA variable.
When object belonging to the class comes across a method call it first searches in the current class(package),if not available it searches in the class specified in the @ISA variable left to right.
If not found also in the @ISA variable ,then only throws a error.
In case of Perl,multiple inheritance is also possible.

Take this perl program for example:


#here the package A has the new constructor which is responsible for creating the Perl object.Here it creates #anonymous hash by using "{}" and blessing into package which comes through shift.
#it also has a subroutine A_method
package A;
sub new { bless {}, shift };
sub A_Method {
print "A_Method\n";
}


package B;
#here the inherutance is defined and @ISA of package B is assigned to A.This makes Package B to inherit
# from A.It has two sub-routines.
@ISA=qw(A);
sub B_Method {
print "B_Method\n";
}
sub Method {
print "Method (in B)\n";
}


package C;
#here @ISA is assigned to B. Now the Package C inherits both from B and A as B inherits from A.It also #has two subroutines.
@ISA = qw(B);
sub C_Method{
print "C_Method\n";
}
sub Method {
print "Method (in B)\n";
}


package main;
# By this method a object reference of the package is returned.
$a = new A;
#here the method first gets converted into  A::new(A) in the format of class::Method('class','arg1','arg2')
#here the new method of Package A is inherited to return the object reference for B.
$b=new B;
#here the new method of Package A is inherited to return the object reference for B.
$c=new C;
$c->C_Method(); #here it calls C_Method of class C
$c->Method();  #here it calls Method of class C
$c->B_Method();#here it calls inherited B_Method of class B
$b->A_Method();#here it calls inherited A_Method of class A
$a->B_Method();#returns a fatal error as the Object A does not inherit B.

OUTPUT:

G:\perl_programs>perl perlinherit.pl
C_Method
Method (in B)
B_Method
A_Method
Can't locate object method "B_Method" via package "A" at perlinherit.pl line 43.


For better understanding also refer to the below program.

Here there are two Classes Person and Student.
The Package Student inherits the new sub-routine of Class Person to define a Student.


package Person;
sub new
{
my $pkg=shift;
bless{ @_ },$pkg;
}
package Student;
# here the Student inherits the Person Package.
@ISA=qw(Person);
$ddd=new Person(name=>"jjjjj",age=>55);
$aaa=new Student(name=>'ssss',id=>55,class=>"tenth");
print $ddd->{name}; # here it prints the name of the Person
print $aaa->{name}; # here it prints the name of the Student.

Output:

jjjjj
ssss

Wednesday, June 15, 2011

use vs require in perl

Use  and require both are used to load the modules with slight differences..

Use:
1)The loading the modules takes place at the compile time.
20The use can be used only to load the module.i.e. the .pm file.
3)the file Extensions should not be specified.
It will be used as
use sample 
if the module name is sample.pm

Require:
1)In case of require the module loading takes place at the run time.
2)The require can be used to load both module and .pl file.
3)The file Extensions has to be specified.
It will be used as
use sample.pm 
if the module name is sample.pm.


use export in perl

Exporter is generally used to import the variables and functions of the modules into the user's namespace.Here there is no need to declare the object and call the function of the modules using that object.

These are the things which have to be included in the module from where you need to export:
use Exporter.
@ISA=qw(Exporter);
@EXPORT = qw(add subtract);
@EXPORT_OK=qw(multiply divide);

Here use Exporter loads the module into the user namespace at the compile time.
@ISA is similar to the inheritance.
Here we add Exporter to the @ISA array.
@EXPORT contains the list of symbols which are exported into the user's namespace.
@EXPORT_OK contains the list of symbols which can be exported if requested by the user.
How really the export takes place:
For example:
consider the module example.pm:

use Example;
use Exporter.

@ISA=qw(Exporter);
@EXPORT = qw(add subtract);
@EXPORT_OK=qw(multiply divide);
sub add
{
($no1,$no2)=@_;
$no3=$no1+$no2;
return $no3;
}
sub subtract
{
($no1,$no2)=@_;
$no3=$no1-$no2;
return $no3;
}
sub multiply
{
($no1,$no2)=@_;
$no3=$no1*$no2;
return $no3;
}
sub divide
{
($no1,$no2)=@_;
$no3=$no1/$no2;
return $no3;
}
1;# this important to signify the end of the package.

Let us use  this module in another file sample.pl:

Content of sample.pl to use Export Module:


# in this case only the default subroutines add and subtract will be added to the user's namespace
use Example;
#But when you need multiply and divide subroutines you need to specify as these are available only on #demand basis in @EXPORT_OK.
use Example qw(multiply divide);
# once it is imported into the namespace you can directly use it
$ss=add(10,50);
print "addition results are:$ss";
#In similar way all the subroutines can be called
The significance of @ISA here is :when the perl program or module cannot find the perl program in the current module ,it will search for in the modules added in the @ISA array before throwing any error.

How this works:
When the sample.pl loads the module Example.pm  the import module in the Example.pm is called automatically making  the functions and variables to be added into the user's namespace.