content of perl_case.pl:
$name="HhEesssSSSSS";
#converting into uppercase
$capital=uc($name);
print "the name in uppercase is $capital.\n";
$lowercase=lc($name);
print "the name in lowercase is $lowercase.\n";
OUTPUT:
G:\PERL_P~1>perl perl_case.pl
the name in uppercase is HHEESSSSSSSS.
the name in lowercase is hheessssssss.
G:\PERL_P~1>
perl_fileout.pl:
open(FILE,">>hello_out.dat");
print FILE "hello howw are you";
print FILE "wonderful";
print FILE "awesome";
print FILE "great";
close(FILE);
G:\perl_programs>perl perl_fileout.pl
G:\perl_programs>
When you run this program hell_out.dat is created in the current directory with the following content:
hello howw are youwonderfulawesomegreat
the following program for making lines come in a new line:
open(FILE,">>hello_out.dat");
print FILE "hello howw are you"."\n";
print FILE "wonderful"."\n";
print FILE "awesome"."\n";
print FILE "great"."\n";
close(FILE);
content of hello.dat:
hello how are you
wonderful beautiful
great
award great
awesome
content of Perl file:
perl_file1.pl
open(FILE,"<hello.dat");
@lines=<FILE>;
close(FILE);
foreach(@lines)
{
chop;
print; # it is same as print $_;
print "\n";
}
output:
G:\perl_programs>perl perl_file.pl
hello how are you
wonderful beautiful
greataward great
awesom
G:\perl_programs>
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>
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>
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
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>