I relented since I am really busy now and am not in the pink of my health.

I am to talk about perl.

This article has all the gory details. I shall talk about trickle in
some other language tip.

For now, take a look at this:

http://aplawrence.com/Girish/tie-file.html

I am yanking the text for those of you who can't access Internet but
can access mail. ;)

The fun with perl is that its manuals are very verbose. Usually it
hurts more than helps since you waste a lot of time to learn
something. In most cases you want to get your job done and you don't
have time to learn everything. You want to use the best tool for the
job and save time to get to the juicier parts of your work.

Your work could mean product development or a quick project. We find
that the ability to manipulate configuration files usually stored
under the /etc directory using a scripting language like perl eases
development in a big way.

I used to do this by hand in perl. It was painful and error prone.
There was no way I could cover all cases and I was looking for a
better way to do things though I mostly gave up on this. That is when
I got to know about Tie::File perl module. This is a delightful way to
do in place editing of files. You don't have to save a copy in /tmp
under a unique file name and then delete it. Truly delightful
experience. Let us look at a sample.

   use Tie::File;

   tie @array, 'Tie::File', filename or die ...;

   $array[13] = 'blah';     # line 13 of the file is now 'blah'
   print $array[42];        # display line 42 of the file

   $n_recs = @array;        # how many records are in the file?
   $#array -= 2;            # chop two records off the end

   for (@array) {
   s/PERL/Perl/g;         # Replace PERL with Perl everywhere in the file
   }

   # These are just like regular push, pop, unshift, shift,
   # and splice
   # Except that they modify the file in the way you would
   # expect

   push @array, new recs...;
   my $r1 = pop @array;
   unshift @array, new recs...;
   my $r2 = shift @array;
   @old_recs = splice @array, 3, 7, new recs...;

   untie @array;            # all finished


I got this straight from the Tie::File man page. This is a case in
point. Nearly every perl module like Digest::SHA1 or DBI come with a
neat way to use their API. The perl online manuals are flush with
examples that straight away illustrate how to use the facilities
offered by the great modules found in CPAN.

Let us get back to our good old problem of file editing.

This line begins our process.

      tie @array, 'Tie::File', filename or die ...;


The text file gets automatically sucked into the perl array @array
with each element of the array containing a line from the file. In our
case we want to edit configuration files. So let us say that we wish
to modify the file /etc/resolv.conf and add a public nameserver to the
list already present.

This line will eminently do the job.

        push @array, "nameserver 4.4.4.3";


The file gets written back when you untie the array. Here is the
complete program.

    use Tie::File;
    tie @resolvarray, 'Tie::File', "/etc/resolv.conf" or
      die "Could not open /etc/resolv.conf file for writing, are you root?";
    push @resolvarray, "nameserver 4.4.4.3";
    untie @resolvarray or die "Could not close file";


I intentionally gave a very simple example. We could do the same thing
with massive text files and do inplace editing if we know in advance
which line number corresponds to which configuration parameter.
Several examples like the Apache httpd.conf, ntp.conf, dict.conf come
to mind.

I personally used Tie::File for my web interface for my spam control
product. The backend CGI used this. I also use Tie::File for
manipulating text files using my nCurses UI. The code is the same. You
could build the input values from different sources.

Perl comes bundled with several useful modules in its default install.
However if you wish to find if a particular module is installed and
available, you could try this.

$ perl -e 'use Tie::File;'


Perl is the only programming language with a centralized well managed
CPAN archive. The repository is so excellently managed that its search
features, the code quality found in modules and the other aspects of
its ecosystem works remarkably well. What irritates me is the perl
-MCPAN switch that installs modules from the command line. It asks so
many questions that leaves me tired and frustrated. Instead I find
that in BSD operating systems many popular perl modules are available
as readymade packages.

-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com
_______________________________________________
To unsubscribe, email [email protected] with 
"unsubscribe <password> <address>"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to