In this tutorial I am talking about file editing in the backend as
that is what gets things going when you
write a web interface.
Even if you are developing a user interface for making your product
user friendly the main backend operation
is file editing.
So I have done quite a bit of this since the general mime for my
product is web panel for configuration
and statistics and all the backend file manipulations are performed
by user input in the web panel.
Editing a file can be done in many languages, even C. But who will
write a C CGI?
I use perl and use it extensively. And you can directly edit a file
using perl CGI.
But I did something better.
I use Tie::File which is a standard perl module for manipulating files.
Basically it ties the file into a perl array using which you can
remove lines, add lines or edit lines
using the same perl functions you use for array elements.
This does not make things really simple but I live with it.
I don't think there is a better way.
How do you do it?
I also do one more thing. When you save a value in the web interface,
the next time user clicks at
that page the old value should be displayed. So editing a file also
means parsing it to display correctly.
I use Tie::File here also; except that I open the file in read only mode.
Here is a simple sample.
use Tie::File;
tie @f, "Tie::File", "/etc/passwd", mode => O_RDONLY;
for(@f) {
print;
}
untie @f;
This will print the file contents. But you want only the patterns you
are interested in
to show in a web interface.
Inside the for() loop you have to use regex.
for(@f) {
if(/foo/) {
($dummy, $val) = split / /;
}
}
Here, $val gets assigned to the value of interest.
Okay now let us get to editing it.
use Tie::File;
tie @f, "Tie::File", "example.conf";
for(@f) {
if(/set/) {
$_ = "changed line";
}
}
untie @f;
Remember, only when you untie the file,the internal array
representation is written back into the disk.
You can use array operations like appending lines to a file, deleting
lines using:
push @f, "another line";
or
@f = @f[1..3];
Lot of possibilities exist.
-Girish
--
Gayatri Hitech
http://gayatri-hitech.com
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc