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.

Tuesday, June 14, 2011

sudo -l not working

sudo -l is a substiitute for profile -l in Solaris.

sudo -l  just prints the commands for which the user has permission to run.

The general way to do it is:

su to the corresponding user and type sudo -l

su user

It will print all the commands the user has perminission to run.

sudo -l   username 
will not work in Linux though profile -l username works for Solaris.

If you get error that the user is not in sudoers or permission denied.

Then it says this particular user is not in the /etc/sudoers.
Check whether  user is present  in any of USER_ALIAS in the /etc/sudoers .
If not add the user to a  appropriate group(USER_ALIAS) in the sudoers file.

For immediate requirement you can also find the  command list by manually tracing  through  the /etc/sudoers file for the user.


netstat getnameinfo failed error

netstat -pt shows getname info failed error when u try to grep something from the output.

the error is due to one of the IP's in the netstat output cannot be converted into hostname as it does not exist in the /etc/hosts instead it is getting converted to UNKNOWN.

You avert the error by using


1) netstat -pt --numeric_ports  | grep something
when you use this command the IP's will not  be converted into hostnames and the error will not arise.

2)or try to find for which IP's the hostname is not generated and in the netstat  output it shows as UNKNOWN and try to add those IP's to the /etc/hosts.
And in some cases it may also be tunnel IP's which is unknown to the system and it will be of different format.