Sunday, July 10, 2011

Callig parent method from child class


Perl Script:


package Inventory_item;
sub new {
$class=shift;
%params=@_;
bless {
"PART_NUM" => $params{"hh"},
"PART_VAL" => $params{"qq"}
},$class;
}
package Coloritem;
@ISA=("Inventory_item");
@PARENT::ISA=@ISA;  # this is the statement needed for calling parent method from child class
sub new {
my($class)=shift;
my(%params)=@_;
my($self)=$class->PARENT::new(@_);
$self->{"INK_COLOR"}=$params{"INK_COLOR"};
return($self);
}
package main;
$item=Coloritem->new("hh","22222","qq","3232323232","INK_COLOR","54545555");
print("The part num is ".$item->{'PART_NUM'} ."\n");
print ("The quantity is ".$item->{'PART_VAL'} ."\n");
print ("the  ".$item->{'INK_COLOR'}."\n");


OUTPUT:


G:\perl_programs>perl perl_parent.pl
The part num is 22222
The quantity is 3232323232
the  54545555
G:\perl_programs>

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>