Archive for the ‘Perl’ Category

Perl: Perl program to find all the lines containing a particular word, from a file.

April 3, 2015

Below is a program to find all the lines containing word ‘pass’, from a file test.txt.

————————————————————————————————–

use strict;
use warnings;

# test.txt contents
# 111: PASS
# 121:pass
# 3212:fail
# 4571:pass
# 876:fail
# 6654:pass

open(my $fh, “<test.txt”);

while (<$fh>)

{
if ($_ =~ /pass/i)
{
print $_;
}
}

# Output:
# 111: pass
# 121:pass
# 4571:pass
# 6654:pass

————————————————————————————————–

Advertisements

Perl program to read a directory recursively and locate files with name matching a string provided.

December 14, 2014

Perl program to read a directory recursively and locate files with name matching a string provided.

————————————————————————————————————————-

————————————————————————————————————————-

use strict;
use warnings;

my $dir_name = shift;
my $search_str = shift;

&Dir_Files (dir => $dir_name, search_str => $search_str);
sub Dir_Files {
my %args = @_;
my $directory = $args{dir};
my $search_str = $args{search_str};
# my $directory = ‘c:\windows’;

opendir(DIR,$directory);
my @files = readdir(DIR);
closedir(DIR);
foreach(@files){
if ($_ =~ /$search_str/i) {
print “File name with $search_str found: “;
}
print $_,”\n”;
}
}

————————————————————————————————————————-

————————————————————————————————————————-

Perl program to read Files in a directory and print their names.

December 14, 2014

Perl program to read Files in a directory and print their names.

——————————————————————————————————————————

——————————————————————————————————————————

use strict;
use warnings;

my $dir_name = shift;

&Dir_Files (dir => $dir_name);
sub Dir_Files {
my %args = @_;
my $directory = $args{dir};
# my $directory = ‘c:\windows’;

opendir(DIR,$directory);
my @files = readdir(DIR);
closedir(DIR);
foreach(@files){
print $_,”\n”;
}
}

——————————————————————————————————————

——————————————————————————————————————

Perl program to read file and count the occurrence of a string and print the number of occurrences.

December 14, 2014

Perl program to read a file content and search for a name in each line, count occurrence of the name (storing data in a hash) and print the statistics (line# and number of occurences)

—————————————————————————————–

—————————————————————————————–

use strict;
use warnings;

my $file = “1.txt”;
my $search_str = “kedar”;

&count_strings_in_file(file=>$file,str=>$search_str);

sub count_strings_in_file {
my %args = @_;
my $file = $args{file};
my $search_str = $args{str};

my $FH;
open ($FH, ‘<‘,$file) or die “Couldn’t open file: $!”;
my $count = 0;
my $line_no = 0;
my $hash = {};

while (<$FH>)
{
$line_no++;
if ($_ =~ /$search_str/i)
{
my @arr = split(” “,$_);
foreach my $word (@arr) {
if ($word eq $search_str) {
$count++;
}
}
}
$hash->{$line_no} = $count;
$count = 0;
}

foreach my $key (keys $hash) {
print “\nThis is line no.: ” . $key . ” and these many occurences: ” . $hash->{$key};
}

}

————————————————————————————————————-

————————————————————————————————————-

Perl program to read a file, search a string in it and print the line having that string.

December 14, 2014

———————————————————————–

———————————————————————–

use strict;
use warnings;

my $file = “1.txt”;
my $search_str = “bye”;

&search_string_in_file(file=>$file,str=>$search_str);

sub search_string_in_file {
my %args = @_;
my $file = $args{file};
my $search_str = $args{str};

my $FH;
open ($FH, ‘<‘,$file) or die “Couldn’t open file: $!”;

while (<$FH>)
{
if ($_ =~ /$search_str/i)
{
print “\n $_ “;
}
}
}

———————————————————————–

———————————————————————–

Perl: Sending 2 arrays as input to subroutine

September 21, 2014

In Perl it is little tricky to send 2 arrays as input to any subroutine. If you send them as arrays, they both will be sent as array lengths and not actual array values. So we have to send their references and de-reference them inside sub routine.

Below is the program to explain it.

——————————————————————————————————————————

#array_ref.pl

use strict;

use warnings;

my @arr1=(“a”,”b”,”c”,”d”);

my @arr2=(“w”,”x”,”y”,”z”);

my $arr1_ref=\@arr1;

my $arr2_ref=\@arr2;

&deref_sub ($arr1_ref,$arr2_ref);

sub deref_sub {

my $ref1=shift;

my $ref2 = shift;

my @inp1=@$ref1;

my @inp2=@$ref2;

foreach my $arr1_val (@inp1) { print (“\n $arr1_val \n”); }

foreach my $arr2_val (@inp2) { print (“\n $arr2_val \n”); }

}

———————————————————————————————————————-

Perl : CGI example

April 25, 2013

Perl is used in most of the companies for web servers requests which deals with GET and POST methods.

To learn the Perl CGI module we will need a web server and perl CGI module installed.

On linux machine, we will need to search for httpd rpm using the below command.

#rpm -q httpd

If rpm is installed then start the httpd service using command

#service httpd start

If httpd rpm is not installed then you can install it from your linux OS CD or using yum command.

#yum install httpd

Now you can create a sample html file under ‘/var/www/html’ directory which will contain all the html pages to be displayed on the http server.

One sample html file is as below:

——————————————————————————

<html>

<body>

<h1> This is heading </h1>

<p> This is paragraph </p>

<A HREF = “http://localhost/cgi-bin/cgi-hello.plx”>hello</A&gt;

</body>

</html>

——————————————————————————–

You can copy the above code and save under /var/www/html as test.html.

Now we need to create cgi-hello.plx file and save it under /var/www/cgi-bin directory.

Below is the cgi-hello.plx file which you can use.

———————————————————————————

#!/usr/bin/perl

use strict;

print “Content-type: text/html \n\n”;

print “<head>Hello CGI world</head>”;

print “<h1>You are calling $ENV{SCRIPT_NAME}</h1>”;

print “<h2>You are using $ENV{REQUEST_METHOD} method</h2>”;

——————————————————————————————-

Now open the web browser and enter address as : http://localhost

Click on the link test.html and then link hello in it.

Output of cgi-hello.plx should be displayed.

Perl: fibonacci series

April 19, 2013

#fibonacci.pl

use strict;

print “\n\nEnter a number, 10 fibonacci series elements starting from that number will be displayed.”;
my $fbc = <STDIN>;
chomp ($fbc);
print “\n you entered $fbc\n”;
my $fb1=0;

for (0..10)
{
my $fb3= $fb1+ $fbc;
$fb1= $fbc;
$fbc=$fb3;
print “$fb3\t”;
}

Perl: Difference between my and local

April 19, 2013

This is a very basic confusion for any one using perl. Now a days local is hardly used by anybody but still it exists in perl and hence the confusion still remains.

The below script will try to remove the confusion in use of my and local variables.

Basically, when you create a ‘my’ type of variable for any ‘global’ variable, you can still access the value in global variable by using ‘$::’ operator.

———————————————————————–

#my_vs_local.pl

use strict;

our $myvar = “global my variable”;
our $mlocal = “global local variable”;

print “\nOutside subroutine: \n$myvar \n$mlocal”;

sub chk {
my $myvar = “inside subroutine my variable”;
local $mlocal = “inside subroutine local variable”;
print “\n\nNow Inside subroutine:”;
print “\n\n$myvar”;
print “\n\$\:\:myvar variable value is \-\> $::myvar”;
print “\n\n$mlocal”;
print “\n\$\:\:mlocal variable value is \-\> $::mlocal”;
}
&chk ;
print “\n\nAgain outside subroutine: \n$myvar \n$mlocal”;

——————————————————————————-

Perl: Regular expression for email address

April 18, 2013

There are many options and variations available for creating regular expression for email address.

However I am mentioning 2 of them which can be used for general purpose.

The first regex will accept email address in any of the below format:

someone@example.com, some_one@example.com or some.one@example.com or some-one@example.com and so on.

The second regex will accept email address in only below 4 formats:

some_one@example.com, some.one@example.com, some+one@example.com,some-one@example.com

—————————————————————————-

#mail_address_validation.pl

print “\nEnter an email address:”;
my $mailadr = <STDIN>;

if ($mailadr =~ ((/^\S+@\S+.\S+$/) ))
{ print “\nValid email address.”;
}
else {print “\nNot cool”;}

if ($mailadr =~ (/^\w+[_.+-]\w+@\w+\.\w+$/))
{ print “\nValid email address”; }
else {print “\nNot cool”}

————————————————————————————