PDA

View Full Version : Text files with Perl


ricktrip
08-04-2005, 05:49 PM
I have written some perl scripts that quiz students online, recording their answers in data files. I've been using the module DB_File to store this data in a "hash," but for various reasons I'd prefer to store it in text files. In particular, after several months of use, one of my files got a single byte written wrong, rendering the file useless. With a text file, I could look at it in my editor to check for creeping corruption, and do a fix by hand.

Reading and writing text files in perl isn't hard, but there will be a lot of data and it is important that it not be lost. Is there a module I should be using that takes care of backups or write-verification, or is that overkill? Perhaps I just need a strategy for saving prior versions of each file in case I need to examine them. I'd appreciate any and all advice!

Thanks!

Rick Triplett

Kevin
08-04-2005, 06:04 PM
The biggest problem with using text files is when two people punch submit at the same time they can corrupt the file.

I would personally use MySQL (which comes with your account) but that may also be considered overkill.

ricktrip
08-04-2005, 06:30 PM
Thanks for the suggestion, Kevin. I have given some thought to mySQL. It may be overkill, but as my student base grows, perhaps it's a good idea. I assume that the use of mySQL is the best way to maintain data integrity, I've just been afraid of the learning curve (Perl's curve has kept me pretty busy). Is there a reference for beginners that provides a gentle introduction to mySQL? Perhaps I could begin dabbling with it, then incorporate it into my student data needs a few months down the line.

Rick Triplett

Kevin
08-05-2005, 11:32 AM
This is what I started with: http://www.amazon.com/exec/obidos/tg/detail/-/1565924347/qid=1123252312/sr=8-1/ref=pd_bbs_1/102-6463798-7833747?v=glance&s=books&n=507846

However this book is newer and better: http://www.bookpool.com/sm/0596002114

ricktrip
08-12-2005, 05:05 PM
Thanks, Kevin; I ordered the book you recommended and it came today. Thanks to you I am entering a new, interesting, and useful world of computing.

Rick

Satanicpuppy
08-30-2005, 05:02 PM
If you want to stick to text files with perl, and avoid the expense/bother/whatever of setting up or obtaining sql hosting, you can use flock() to lock your text files for writing--it should solve the whole corruption issue.

MySQL is a perfectly decent solution though.

Kevin
08-30-2005, 05:14 PM
If you want to stick to text files with perl, and avoid the expense/bother/whatever of setting up or obtaining sql hosting, you can use flock() to lock your text files for writing--it should solve the whole corruption issue.

MySQL is a perfectly decent solution though.
Actually, flock() is a perfectly valid solution for something simple like this however this kind of simple task is the perfect excuse to learn MySQL which is usefull for much bigger things :yeah:

Satanicpuppy
09-21-2005, 12:55 PM
Actually, flock() is a perfectly valid solution for something simple like this however this kind of simple task is the perfect excuse to learn MySQL which is usefull for much bigger things :yeah:

Heh. I hear ya. I'm a big SQL buff myself.

I've run across a few sites though, where I could get Perl support and not SQL support, so I thought I'd throw it out there. Sometimes a flat file is all you need...Generally though that's when you're limited to Javascript, and then you have to do all the work by hand *shudder*