Friday, 14 June 2013

Perl examples

1) A perl program to read a line  from standard input and  output each words entered using foreach.


$str1 = <STDIN>;

chomp $str1;

$count =1;

foreach $chr1 (split( //,$str1)){

print "\n word $count = $chr1";



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


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.


$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.


$str = <STDIN>;

if ( $str =~ /^\d+$/ || $str =~ /^[0-9]+$/)


print "\nnatural number";


elsif ( $str =~ /^[+-]?\d+$/ || $str =~ /^-?\d+$/)


print "\nInteger";




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.


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


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.


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.


@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.


print "\n $para[1]";

9) A perl program showing different ways of  storing variables in array.


@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.

@arr1 = qx(ls);

print "$arr1[0],$arr1[1],$arr1[2]";

11) A perl program to reduce the  length of an array using $#ARRAY.

@vararr = qw(This is an array);

$x = $#vararr;
print "\n|@vararr| length = " . ($x + 1);
$#vararr -= 1;
$x = $#vararr;
print "\n |@vararr| - length = " . ($x + 1);


  1. Well It Was Very Good Information For All Linux Learners.We Also Provide All Linux Online Training Courses In World Wide.

  2. Nice stuff and Great work. You are doing a good job. We also provide Unix shell scripts stuff

  3. This information you provided in the blog that is really unique I love it!! Thanks for sharing such a great blog. Keep posting..
    Unix training in Noida
    Unix training institute in Noida
    Unix Course in Noida

  4. great post
    Indian Cyber Army’s most awaited internship is live now. Rush to register for Summer Internship 2018 on “ Ethical hacking” and book your seats before it runs out.Candidates have to get themselves registered to be a part of this Internship program. As career in ethical hacking is most in demand.

  5. Thanks for the information.It is really nice .In this age of Technology advancement, computer and information technology have not only brought convenience to citizens in modern life but also for policemen & various Government officials of the nation to fight cybercrime through various modus operandi. Indian Cyber Army has been dedicated in fighting cyber crime, striving to maintain law and order in cyberspace so as to ensure that everyone remains digitally safe.Read more:- Information Security

  6. thanks for sharing the more valuable information.
    Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.Ethical hacking training ,
    Ethical hacking course