1) A perl program to read a line from standard input and output each words entered using foreach.
#!/usr/bin/perl
$str1 = <STDIN>;
chomp $str1;
$count =1;
foreach $chr1 (split( //,$str1)){
print "\n word $count = $chr1";
$count++;
}
2) A perl Program to reverse an entered input of line.
a) Reverse the line of string as a whole.
b) reverse each words in the line
#!/usr/bin/perl
print "\n enter string";
$var1 = <STDIN>;
print "\n enter string";
$var2 = <STDIN>;
$rev1 = reverse($var1);
$rev2 = join ( " ",reverse split(" ",$var2));
print "\n reverse of $var1 = $rev1";
print "\n reverse of $var2 = $rev2";
3) Perl program containing substitution of expressions containing variables within patterns.
#!/usr/bin/perl
$no = 25;
$str1 = 'MY NUM IS $no';
$str2 = 'MY NUM INTO 2 IS $no';
$str1 =~ s/\$([a-z]+)/${$1}/g;
$str2 =~ s/\$([a-z]+)/${$1} * 2/eg;
print "\n $str1";
print "\n $str2";
4)Perl program to check whether input number is a natural number or integer.
#!/usr/bin/perl
$str = <STDIN>;
if ( $str =~ /^\d+$/ || $str =~ /^[0-9]+$/)
{
print "\nnatural number";
}
elsif ( $str =~ /^[+-]?\d+$/ || $str =~ /^-?\d+$/)
{
print "\nInteger";
}
else
{
print "\nnot an integer"
}
5)Perl program to add or subtract days from an entered date in YYYY-MM-DD format.It requires the Date::calc module.
#!/usr/bin/perl
use Date::Calc qw(Add_Delta_Days);
print "\nenter date in YYYY-MM-DD ";
$dt = <STDIN>;
print "\nEnter days to add or -days to subtract ";
$of = <STDIN>;
@darr = split("-",$dt);
$yr = $darr[0];
$mon = $darr[1];
$ddi = $darr[2];
printf( "\nnew date = %04d-%02d-%02d",Add_Delta_Days($yr,$mon,$ddi,$of) );
6) A perl program to print no of days, hours, minutes & seconds between two given days
#!/usr/bin/perl
use Date::Calc qw(Delta_Days Delta_DHMS);
@date1 = (2004,03,13);
@date2 = (2003,12,15);
$ddiff = Delta_Days(@date2,@date1);
print "\n The days between the two dates $ddiff";
@dtim1 = (2005,06,17,14,36,17);
@dtim2 = (2005,06,18,11,19,35);
@dtimdiff = Delta_DHMS(@dtim1,@dtim2);
print "\n $dtimdiff[0] days $dtimdiff[1] Hrs $dtimdiff[2] Mins $dtimdiff[3] Secs";
.
7) A Perl program to add a specified days,hours,minutes,secinds to a given date and time.
#!/usr/bin/perl
use Date::Calc qw(Add_Delta_DHMS);
print "\nenter Date & Time in YYYY-MM-DD-HH-MI-SS ";
$dt = <STDIN>;
print "\nEnter duration to add in DAYS,HRS,MIN,SS ";
$of = <STDIN>;
@dtmarr = split("-",$dt);
$yr = $dtmarr[0];
$mon = $dtmarr[1];
$ddi = $dtmarr[2];
$hh = $dtmarr[3];
$mi = $dtmarr[4];
$ss = $dtmarr[5];
@durarr = split(",",$of);
$days = $durarr[0];
$hrs = $durarr[1];
$mins = $durarr[2];
$secs = $durarr[3];
printf( "\nnew date = %04d-%02d-%02d %02d:%02d:%02d",Add_Delta_DHMS($yr,$mon,$ddi,$hh,$mi,$ss,$days,$hrs,$mins,$secs) );
8) A perl program to read a paragraph into an array and then print a particular line.
#!/usr/bin/perl
@para = (<<"INPUT" =~ m/^\s*(.+)/gm);
It was a day, long and hectic,
the day which I slogged more than ever
but got no reward and no accolades.
INPUT
print "\n $para[1]";
9) A perl program showing different ways of storing variables in array.
#!/usr/bin/perl
@arr1 = "what are you doing"; #strores entire string as one element;
@arr2 = ("what","are","You","doing"); # stores quoted strings as individual elements
@arr3 = qw (what are you doing ?); # stores individual words as elements
print "\n $arr1[0]";
print "\n $arr1[2]";
print "\n $arr2[2]";
print "\n $arr3[2]";
10)A perl progrm to store output of Unix ls command into an array.
#!/usr/bin/perl
@arr1 = qx(ls);
print "$arr1[0],$arr1[1],$arr1[2]";
11) A perl program to reduce the length of an array using $#ARRAY.
#!/usr/bin/perl
@vararr = qw(This is an array);
$x = $#vararr;
print "\n|@vararr| length = " . ($x + 1);
$#vararr -= 1;
$x = $#vararr;
print "\n |@vararr| - length = " . ($x + 1);
#!/usr/bin/perl
$str1 = <STDIN>;
chomp $str1;
$count =1;
foreach $chr1 (split( //,$str1)){
print "\n word $count = $chr1";
$count++;
}
2) A perl Program to reverse an entered input of line.
a) Reverse the line of string as a whole.
b) reverse each words in the line
#!/usr/bin/perl
print "\n enter string";
$var1 = <STDIN>;
print "\n enter string";
$var2 = <STDIN>;
$rev1 = reverse($var1);
$rev2 = join ( " ",reverse split(" ",$var2));
print "\n reverse of $var1 = $rev1";
print "\n reverse of $var2 = $rev2";
3) Perl program containing substitution of expressions containing variables within patterns.
#!/usr/bin/perl
$no = 25;
$str1 = 'MY NUM IS $no';
$str2 = 'MY NUM INTO 2 IS $no';
$str1 =~ s/\$([a-z]+)/${$1}/g;
$str2 =~ s/\$([a-z]+)/${$1} * 2/eg;
print "\n $str1";
print "\n $str2";
4)Perl program to check whether input number is a natural number or integer.
#!/usr/bin/perl
$str = <STDIN>;
if ( $str =~ /^\d+$/ || $str =~ /^[0-9]+$/)
{
print "\nnatural number";
}
elsif ( $str =~ /^[+-]?\d+$/ || $str =~ /^-?\d+$/)
{
print "\nInteger";
}
else
{
print "\nnot an integer"
}
5)Perl program to add or subtract days from an entered date in YYYY-MM-DD format.It requires the Date::calc module.
#!/usr/bin/perl
use Date::Calc qw(Add_Delta_Days);
print "\nenter date in YYYY-MM-DD ";
$dt = <STDIN>;
print "\nEnter days to add or -days to subtract ";
$of = <STDIN>;
@darr = split("-",$dt);
$yr = $darr[0];
$mon = $darr[1];
$ddi = $darr[2];
printf( "\nnew date = %04d-%02d-%02d",Add_Delta_Days($yr,$mon,$ddi,$of) );
6) A perl program to print no of days, hours, minutes & seconds between two given days
#!/usr/bin/perl
use Date::Calc qw(Delta_Days Delta_DHMS);
@date1 = (2004,03,13);
@date2 = (2003,12,15);
$ddiff = Delta_Days(@date2,@date1);
print "\n The days between the two dates $ddiff";
@dtim1 = (2005,06,17,14,36,17);
@dtim2 = (2005,06,18,11,19,35);
@dtimdiff = Delta_DHMS(@dtim1,@dtim2);
print "\n $dtimdiff[0] days $dtimdiff[1] Hrs $dtimdiff[2] Mins $dtimdiff[3] Secs";
.
7) A Perl program to add a specified days,hours,minutes,secinds to a given date and time.
#!/usr/bin/perl
use Date::Calc qw(Add_Delta_DHMS);
print "\nenter Date & Time in YYYY-MM-DD-HH-MI-SS ";
$dt = <STDIN>;
print "\nEnter duration to add in DAYS,HRS,MIN,SS ";
$of = <STDIN>;
@dtmarr = split("-",$dt);
$yr = $dtmarr[0];
$mon = $dtmarr[1];
$ddi = $dtmarr[2];
$hh = $dtmarr[3];
$mi = $dtmarr[4];
$ss = $dtmarr[5];
@durarr = split(",",$of);
$days = $durarr[0];
$hrs = $durarr[1];
$mins = $durarr[2];
$secs = $durarr[3];
printf( "\nnew date = %04d-%02d-%02d %02d:%02d:%02d",Add_Delta_DHMS($yr,$mon,$ddi,$hh,$mi,$ss,$days,$hrs,$mins,$secs) );
8) A perl program to read a paragraph into an array and then print a particular line.
#!/usr/bin/perl
@para = (<<"INPUT" =~ m/^\s*(.+)/gm);
It was a day, long and hectic,
the day which I slogged more than ever
but got no reward and no accolades.
INPUT
print "\n $para[1]";
9) A perl program showing different ways of storing variables in array.
#!/usr/bin/perl
@arr1 = "what are you doing"; #strores entire string as one element;
@arr2 = ("what","are","You","doing"); # stores quoted strings as individual elements
@arr3 = qw (what are you doing ?); # stores individual words as elements
print "\n $arr1[0]";
print "\n $arr1[2]";
print "\n $arr2[2]";
print "\n $arr3[2]";
10)A perl progrm to store output of Unix ls command into an array.
#!/usr/bin/perl
@arr1 = qx(ls);
print "$arr1[0],$arr1[1],$arr1[2]";
11) A perl program to reduce the length of an array using $#ARRAY.
#!/usr/bin/perl
@vararr = qw(This is an array);
$x = $#vararr;
print "\n|@vararr| length = " . ($x + 1);
$#vararr -= 1;
$x = $#vararr;
print "\n |@vararr| - length = " . ($x + 1);