A little Perl goes a long way

I have found Perl as very powerful language , a must in the toolkit for any programmer

Here are some Perl code snippets.

It is always better to start your perl code with

use strict;
use warnings;

With this you have to declare variable before you use them

like

my $SearchStep=0;
my $line;

Also if you want to use special Perl modules like for example working with ini files , you
include it

use IniFiles;

Everything is pretty simple in Perl, only thing is that the syntax can become unreadable; so
put in a lot of comments

# This is a comment

Okay let us jump in -- here is the code to open a file

my $FilePath = "system1.txt";
my $opened =open (FH, $FilePath) ;
if( !$opened)
{
print "Cannot open file\n";
die;
}

and to read line by line from the file

my $line;
while ( $line =)
{
chomp ($line);
:

Now to test if a line matches a perl pattern

if($line=~m/$SearchPattern/i)

/i stands for case insensitive search.

Okay a few other matche examples ( basically the better you are at regular expression the more powerfully you can drive perl)

my $regex = join '|', qw(cat,cow,hen);
if($line=~m/\b(?:$regex)\b/i)

(qw- means quote word, basically puts "" )

matching a pettern

if($line=~m/\b(\d\.\d\.\d)\b/i)

Okay now there is another operator ( the substitute operator s// )

Here are some examples for the same

my $the = "The";
$left=~s/^$the\s+//i; #replace "The" with nothing match no case

In case you are creating an XML file these substitutions will be helpful, otherwise
the less than "<" and greater than ">"symbols interfere

$left=~s/\&/and/g; #replace & with 'and' , g - continue till end
#( note special charectes like & is preceeded by the escape charecter \ - \& )
$token=~ s/\s+//g; #replace space with nothing, g - continue till end

$line2=~s/\</& lt;/g; #replace < with <
$line2=~s/\>/& gt;/g; #replace > with >

(note there should be no spaces between '& gt'-- html rendering pblms )


Okay here is another way to take care of all the non word charectes that occur inthe
string and causes wrong string concatination etc.

#the \Q=quote non-word characters till \E
# if( ! ($g_Accumilated=~m/\Q$line2\E/) )

Here is another tool to split up a line on either side of a token

@list =split(/$token/i,$line2);

$left=$'; # the left part of the matched string is in the system variable $'
#right part is see http://www.perl.com/doc/manual/html/pod/perlvar.html

The @ symbols precceds an array variable defenition

To traverse through the array

foreach( (@list) )
{
print ("Item=",$_, "\n");
}

The system variable $_ contains the current variable

What if you want to write a function , you define it as

sub GetToken
{



Okay what if it has to take parameters

sub GetToken
{

my ($line2,$token) = @_; #the parmaters are in the default array variable @_

:

}

and how to call it -

GetToken($line,"Split token");

A few more tokenizing examples

#tokenize the line based on spaces
@tokens = split(/ +/, $line);

Adding one string to another using the . operator

$g_Accumilated = $g_Accumilated . $line2 . "\n";

Okay now on to another powerful container - Perl hash maps

Declaring a hash map

my %pbNameValue = ();

Populating it

$pbNameValue{$tempKey} =$tempVal;


Sorting the hash map

#sort based on the nueric value of the key ie 1 2 10 and not like 1 10 2
my $key;
my %tmpNameValueHash = ();

foreach $key (sort {$a <=> $b } keys %pbNameValue)
{

Iterating through the hash map

foreach $key (keys %tmpNameValueHash )
{










Comments

Alex.C.P said…
Here is another snippet

#!/usr/local/bin/perl

use strict;
use warnings;


my $SearchStep=0;
my $line;
my %firstFileHashMap = ();
my $dummy=1;

my $numArgs = $#ARGV + 1;

if($numArgs != 2)
{
print "Please provide two file names";
exit;
}

my $FilePath = $ARGV[0];
my $opened =open (FH, $FilePath) ;

if( !$opened)
{
print "Cannot open file1 \n";
die;
}

#Store the first file names in a hash map
while ( $line =&ltFH&gt)
{
chomp ($line);
#\HealthCheckController.java@@\m

# .\src\build\serverlist@@\main\13

# print "Line=$line\n";
# if( $line=~m!\\(\w+)\.java@@! )
# if( $line=~m!\\(\w+\.java)@@! ) #got me Utils.java
if( $line=~m!(\.?(\\\w+)+\.java)@@! ) #got me - Java=.\src\webnmc\src\com\nsn\webnmc\common\services\corba\Utils.java
{
# print "Java=$1\n";
#Store it in a hash
#Substitute the filepath with forward slash
my $sub= $1;
$sub=~s/\\/*/g; #replace \ with * --*src*webnmc*src*com*nsn*webnmc*common*services*corba*Utils.java
#print "$sub\n";
$firstFileHashMap{$sub}= $dummy; #key is imp not value

}


}
#Dummy test
#my $key= ".*src*webnmc*src*com*nsn*webnmc*common*services*corba*Utils.java";

#if (exists($firstFileHashMap{$key})) {
# print "Found!!\n";
# } else {
# print "Not found\n";
#}

close(FH);


# Now open the second file and check it the name exists


$FilePath = $ARGV[1];
$opened =open (FH, $FilePath) ;

if( !$opened)
{
print "Cannot open file $ARGV[1]\n";
die;
}

while ( $line =&ltFH&gt)
{
chomp ($line);
#\HealthCheckController.java@@\m
#print "Line=$line\n";
# if( $line=~m!\\(\w+)\.java@@! )
#if( $line=~m!\\(\w+\.java)@@! )
if( $line=~m!(\.?(\\\w+)+\.java)@@! )
{
#print "*********************$line\n";
#print "Java=$1\n";

my $sub= $1;
my $todisplay=$1;
$sub=~s/\\/*/g; #replace \ with * --*src*webnmc*src*com*nsn*webnmc*common*services*corba*Utils.java

if (exists($firstFileHashMap{$sub}))
{
#print "Common-$todisplay\n";
}
else
{
print "Unique-$todisplay\n";
}

}

}

close(FH);
Alex.C.P said…
A very clear tutorial

http://perldoc.perl.org/perlretut.html

Popular posts from this blog

Long running Java process resource consumption monitoring , leak detection and GC tuning

OpenLayers Advanced Clustering and Setting dynamic images for OpenLayer Styles via Ajax

Best practises - Selenium WebDriver/ Java