Easy file editing with Tie::File and perl


2009/09/21
by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:

http://gayatri-hitech.com

http://spam-cheetah.com

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.



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Easy file editing with Tie::File and perl


3 comments



Increase ad revenue 50-250% with Ezoic


More Articles by © Girish Venkatachalam







Mon Sep 21 12:50:51 2009: 6942   TonyLawrence

gravatar
I use that when I need to make mass changes to files here. Usually I'm fixing editing mistakes from some previous mass change :-)





Thu Feb 4 21:23:25 2010: 8029   Six

gravatar


Hello! Great article, clean and simple. Thank you.



Sat Jul 10 03:12:53 2010: 8804   StavanShah

gravatar


Very simple and Informative.
Thanks

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Yeah, it's obsolete, clunky, insecure and broken, but people still use this stuff (Tony Lawrence)

We made the buttons on the screen look so good you'll want to lick them. (Steve Jobs)






This post tagged: