Posts

Showing posts from May, 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

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 exp…