Wednesday, October 21, 2009

Read file by blocks

Perl is very good at reading files lines by lines, using the new line as a separator. You can still define the separator character, by setting the '$/' variable (you can find an example here).
However, when you are reading a log file, with some logs on several lines, the only solution is to parse the text. But here, split comes to the rescue, and you can easily parse.

For instance, if every log is starting with something like '2009/10/21 15:12:01.123 ', and of course if no other line can start with that pattern, here is an easy way :

my $buf="";
local $/=undef;
local *FILE;
open(FILE,"<${file}") or die "Couldn't open file: $!";
$buf = ;
close FILE;

my @logs=split(/(20[0-9]{2}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2} /,$buf);
my $nbLogs=scalar(@logs)/2;

This is perhaps not the more refined way to do it, but it does the job, and it does it well.


Thursday, December 4, 2008

PC 3270 automation - screen scraping

At work, we are working on IBM TPF systems, with access through PC 3270 sessions (IBM Personal communication aka PCOMM). The PCOMM provides an API EHLLAPI with a dll. This allowed us to developp a framework to run Perl scripts to access and modify data on TPF. I did write a code snippet on PerlMonks to help people willing to automate tasks through PC 3270 sessions.


ActivePerl, my Perl on Windows

I mainly work on Windows, and after some study, I finally decided to go for ActivePerl from ActiveState. This is a good Perl distribution on Windows, easy to install and maintain. The Perl Package Manager (PPM) is comfortable to look for new modules and install them. Their repository is pretty up-to-date, but from times to times, you don't find the last version or some specific modules. So I decided to look for an alternate repository, and I found this one. Simply add as a new repository in PPM.


Perl monks, the experts

One of the most useful community of Perl users is PerlMonks. You will find there a impressive quantity of questions, tutorials, code snippets and above all a bunch of Perl coders, friendly and expert skilled, who will take time to help you solve your problems. A must have in the Perl bookmarks for anyone willing to go deeper into Perl odyssey.


CPAN or where to find everything for Perl

One of the great power of Perl is the community. There are out here a lot of people working/playing with Perl, and among them, some share a piece of their production through modules for Perl. If you need something a minimum generic, their is a great chance for you to find it on CPAN, a great repository with lots of good and useful modules.


Why a Perl blog ?

There are on the web a incredible number of Perl tutorials, documentation and reference pages, and I don't feel to have the knowledge and time necessary to create yet another one of quality. However, my Perl endeavor at work gives me the opportunity to have sometimes useful information, so a blog is a good way to share it. That's all.