home about us contact us

free scripts advanced scripts online tools great books web related tutorials contributed tutorials news archive geek toys!

help forum live chat help

Perl File manipulation.

Say for example, you have a script of some sort, that writes data from your users onto a file on your hosting server. (The language that that script uses is irrelevant,, only the fact that it saves its data to a file on your server matters..

You want to pull that data out of the file, and do something with it in Perl... (like format it and incorporate it into a html table or whatever.)

Say for example that the file was called data.txt and it resided on your server in /var/www/cgi-bin/mydata (which on a windows server would probably look like this: c:/Inetpub/Scripts/mydata )

we could write a Perl script that contains this bit of code:

	# Set $data_file to the location and name of the file in question.
my $data_file = '/var/www/cgi-bin/mydata/data.txt';

open DATA, "$data_file" or die "can't open $data_file $!";
	# At this point, Perl has opened the file and we can
	# start working on the data in it.
close (DATA);

That's how you open the file for reading.. (it will not write to the file at the moment, just read only, but that can be changed by adding a couple of modifiers which I'll get into later. but here is the same file, opened for writing:

open DATA, ">$data_file" or die "can't open $data_file $!";

As you can see, the only difference is the > in front of the $data_file variable. but now you can write to the file. Here is a small summary of other modes for the open command.

File Operation modes:

<$file&path     Read access only (The same as not specifying a mode.)
>$file&path     Write access, create if nonexistant, and overwrite existing data.
>>$file&path    Write access, append to existing data.
+<$file&path    Read and Write only, no file creation/appending. Overwrite existing data.
+>$file&path    Read, Write, Create, overwrites rather then appending.
+>>$file&path   Read, Write, Create, Append, no overwriting.
| SYSCOMMAND    Write data to external command only.
SYSCOMAND |     Read data from external command only.

You can open a file for reading, writing, appending and others, you can even tell Perl to create the file if it doesn't already exist.
open is also the command often used to talk to email servers (like sendmail) and other external programs from within a Perl script.

In the above example you can see that I have opened the file specified in $data_file and assigned the opened file a file handle (just a name you make up to associate to the opened file) called DATA, (we could have called it anything we want.) from that point on, you ALWAYS refer the handle you gave the open file, not the file name.. so if you wanted to print to the file, (assuming you had opened it with writing enabled) you would put a line like this between the open and close statements:

print DATA 'This string of text will be written to the open file.';

And Perl will print that string to the file handle DATA, which means it writes it to the opened file.
and if you then opened the file in a txt editor, you'd find the line: This string of text will be written to the open file.
Pretty simple really... :-)

You may have noticed this in the open file line: or die "can't open $data_file $!";
This goes in line with what I told you earlier about always allowing for every possibility when writing if/elsif/else statements.. if we didn't have the or die "can't open $data_file $!"; bit in there, and the file didn't exist, or you didn't have permissions to open it, or the file was in the wrong place.. your script would not work properly, and you would not know why, that bit of code:

open DATA, "$data_file" or die "can't open $data_file $!";

In English, simply says: open the file $data_file, and assigned it to the handle DATA, if I can't open the file, then die (stop running) and return the message "can't open /var/www/cgi-bin/mydata/data.txt' and the $! is just how you tell Perl to mention the line of the script that the problem occurred on and any info it knows about why it couldn't preform the task. so the error message might look like this: can't open /var/www/cgi-bin/mydata/data.txt line 214

Its just a good way of ensuring that you have covered any possible contingency.

Open, is also one method of talking to the mail server on your hosting server when you want your Perl script to send an email, but that's something I will leave till I show you an example. open is really really handy, and I have never written a big script that didn't use open in some form or another to achieve the desired results.

One thing that is also important to cover, is file locking (flock). Say for example your script gets really popular and two people hit it at exactly the same time. Its possible that this could corrupt the data contained in that file or wipe it altogether. Flock allows the script to block access to the file while it is opened. But it can only work properly if all scripts that access the file also use flock.
I have not covered this in detail yet, so in the meantime I will point you to a file locking tutorial on Perl file locking.

So if you are ready to learn more:
Back to the Tutorial Index