Sunday, July 10, 2011

adding a path to the environment

Perl Script:

#ENV is a hash in perl.
#You can access as it a simple hash
#For instance to add a path in the environment variable PATH in the OS execute the following program:


$ENV{'PATH'} = "G:\\perl_programs;"."$ENV{'PATH'}";
print "$ENV{'PATH'}";

OUTPUT:

G:\perl_programs>perl env_path.pl
G:\perl_programs;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Prog
ram Files\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:
\strawberry\perl\bin
G:\perl_programs>

displaying env variables set

Perl Script:

foreach $key (keys(%ENV)) {
    printf("%-10.10s: $ENV{$key}\n", $key);
}

Output:

PROMPT    : $P$G
NUMBER_OF_: 4
FP_NO_HOST: NO
HOMEPATH  : \Documents and Settings\acer
PATH      : C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Pr
iles\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;
wberry\perl\bin
USERDOMAIN: ACER-5D58E53AB3
TERM      : dumb
PROCESSOR_: x86
QTJAVA    : C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TEMP      : C:\DOCUME~1\acer\LOCALS~1\Temp
PROCESSOR_: 2502
SYSTEMDRIV: C:
SYSTEMROOT: C:\WINDOWS
COMSPEC   : C:\WINDOWS\system32\cmd.exe
LOGONSERVE: \\ACER-5D58E53AB3
SESSIONNAM: Console
FTP_PASSIV: 1
WINDIR    : C:\WINDOWS
PROCESSOR_: 6
USERNAME  : acer
PROCESSOR_: x86 Family 6 Model 37 Stepping 2, GenuineIntel
ALLUSERSPR: C:\Documents and Settings\All Users
COMPUTERNA: ACER-5D58E53AB3
CLASSPATH : .;C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TMP       : C:\DOCUME~1\acer\LOCALS~1\Temp

time difference perl objects

Perl Script:

package Timer;
sub new
{
my $self=shift;
$dd={};
$dd{"created"}=time;
print "the time now is $dd{'created'}"."\n";
bless $dd,$self;
}
sub diff
{
my $self=shift;
my $ff= time - $self->{created};
}
package main;
$timer=new Timer;
sleep 4;
$dd=$timer->diff;
print "time elapsed is $dd"

Output:

G:\perl_programs>perl perl_object1.pl
the time now is 1310282015
time elapsed is 1310282019
G:\perl_programs>

sending array as arguments

Perl Script

sub myfunc
{
my @a = @{shift()};
print "@a";
}
package main;
@aa=("hello","aaaaa");
&myfunc (\@aa);

Output:

G:\perl_programs>perl function_sendarray.pl
hello aaaaa
G:\perl_programs>

Saturday, July 9, 2011

inheritance example 2

Perl Script:

#usr/bin/perl
package Person;
sub new
{
my $pkg=shift;
bless{ @_ },$pkg;
}
package Student;
@ISA=qw(Person);
$ddd=new Person(name=>"jjjjj",age=>55);
$aaa=new Student(name=>'ssss',id=>55,class=>"tenth");
print $ddd->{name};
print $aaa->{name};

Output:

jjjjj
ssss

Inheritance example 1

PERL SCRIPT:

package A;
sub new { bless {}, shift };
sub A_Method {
print "A_Methos\n";
}
package B;
@ISA=qw(A);
sub B_Method {
print "B_Method\n";
}
sub Method {
print "Method (in B)\n";
}
package C;
@ISA = qw(B);
sub C_Method{
print "C_Method\n";
}
sub Method {
print "Method (in B)\n";
}
package main;
$a = new A;
#here the method first converted A::new(A) in the format of class::Method('class','arg1','arg2')
$b=new B;
$c=new C;
$c->C_Method();
$c->Method();
$c->B_Method();h
$c->A_Method();
$a->Method();  # this returns error as  object $a does not contain method Method();

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.

Tuesday, July 5, 2011

Debugging in perl

To see the warning present in your perl script run your  script with -w as as follows:

perl -w  karthik.pl

It will help to find out the error in your script.
Next fine way of finding the errors is stepping through the program line by line:
For that you can run the script with  -d option.

perl -d inheritance.pl
These options will be handy for that:
s        -  for moving through the program step by  step.Even when it moves into a function it moves line by line.It is very slow maybe helpful to see how the code is executed.

n       -  it executes the next statement in the script .Although all function calls are executed it does not follow the execution path inside a function.It helps to move quicker than the s option.

c     -  it executes entire script untill it encounters a break point before the script ends.
          you can also use this option to continue untill the line number after c.
          c 40   - makes the debugger to execute untill line number 40.

No command(enter)  - makes it to execute previous n or s command previously executed.
example:
  DB<1> s
main::(perl_inheritance.pl:24): $a = new A;
  DB<1> s
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
  DB<1> s
main::(perl_inheritance.pl:26): $b=new B;
  DB<1> n
main::(perl_inheritance.pl:27): $c=new C;
Here as you may see when you use s option the debugger moves into the new method
but when use the n option it does not go into the execution path of the method and moves next line after the method.

  DB<1> c
C_Method
Method (in B)
B_Method
A_Methos
Method (in B)
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.
when you use c option it simply ends as we have not set any breakpoints


Seeing the trace how the program gets executed:

For that you have to turn on the trace mode:
typing t and pressing enter turns on the trace and typing t again and pressing Enter turns off the trace.
  DB<1> t
Trace = on
  DB<1> c
C::(perl_inheritance.pl:15):    @ISA = qw(B);
C::(perl_inheritance.pl:16):    sub C_Method{
main::(perl_inheritance.pl:24): $a = new A;
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
main::(perl_inheritance.pl:26): $b=new B;
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
main::(perl_inheritance.pl:27): $c=new C;
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
main::(perl_inheritance.pl:28): $c->C_Method();
C::C_Method(perl_inheritance.pl:17):
17:     print "C_Method\n";
C_Method
main::(perl_inheritance.pl:29): $c->Method();
C::Method(perl_inheritance.pl:20):      print "Method (in B)\n";
Method (in B)
main::(perl_inheritance.pl:30): $c->B_Method();
B::B_Method(perl_inheritance.pl:9):
9:      print "B_Method\n";
B_Method
main::(perl_inheritance.pl:31): $c->A_Method();
A::A_Method(perl_inheritance.pl:4):
4:      print "A_Methos\n";
A_Methos
main::(perl_inheritance.pl:32): $c->Method();
C::Method(perl_inheritance.pl:20):      print "Method (in B)\n";
Method (in B)
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid  stopping after program termination,
  h q, h R or h o to get additional info.
  DB<1>

Setting  the breakpoint:

b   - sets the  breakpoint
b followed by the line no sets the breakpoint in that lineno.
  DB<1> b 27
  DB<2> c
main::(perl_inheritance.pl:27): $c=new C;

As you may see i set the breakpoint at line no 27 and use option c it stops at the lineno 27.

D  - using this option deletes all the breakpoints set.you can also delete individual breakpoints using option d  followed by lineno where breakpoint is set.

For setting action:

Action is used when you want to print any  variable or string before a line no:
It can be done using the following syntax:
a  10 print("$count");

By this $count value is printed before line no.
G:\perl_programs>perl -d perl_inheritance.pl
Loading DB routines from perl5db.pl version 1.33
Editor support available.
Enter h or `h h' for help, or `perldoc perldebug' for more help.
B::(perl_inheritance.pl:7):     @ISA=qw(A);
B::(perl_inheritance.pl:8):     sub B_Method {
  DB<1> a 27 print "hello here is the action"."\n"
  DB<2> c
hello here is the action
C_Method
Method (in B)
B_Method
A_Methos
Method (in B)
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.
hello here is the action
A  - deletes all actions.

Options for displaying information:
L -  lists all breakpoints and actions.

l  - prints part of the script
    l  - prints 10 lines of script
    l  <lineno>  - prints the particular line in the script.
    l   5+4  - lists 4 lines starting with line  5
    l   4-8     - lists lines from 4 to 8

S  - prints all function names defined including those in external scripts

T  - prints the stack trace.The trace of the entire program how it gets executed.

V  -  prints the variables defined from all packages and modules loaded

X  -  list variables only in the current package

Finally
q   - can be used to quit from the debugger