Tuesday, May 22, 2007

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


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";

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


/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);

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

matching a pettern


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 )

Total Pageviews