APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Easy file editing with Tie::File and perl

© September 2009 Girish Venkatachalam

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:


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

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

running cheetah
Stop spam dead in its tracks!

Got something to add? Send me email.

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

Printer Friendly Version

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


Inexpensive and informative Apple related e-books:

iOS 10: A Take Control Crash Course

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of Pages

Take Control of iCloud

Take Control of IOS 11

More Articles by © Girish Venkatachalam

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

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


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

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


Very simple and Informative.


Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

If we define Futurism as an exploration beyond accepted limits, then the nature of limiting systems becomes the first object of exploration. (Frank Herbert)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode