Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl monks I come to you with a grave question :)

when you are writting to a database How can I write the new information to the top of the .db file. Instead of the bottom.
Im pretty sure it has to do with seeking and flocking.

flock(fileHandle,2);
seek(filehandle,0,0);
I read in a book that seek(filehandle,0,0);
places it at the end of the file.
So I tried, 0,2, 2,0 It didn't work. Any Suggestions?????

Thanks for any help! :D

Replies are listed 'Best First'.
Re: Problem writting to a database (db)
by Zaxo (Archbishop) on Oct 07, 2003 at 02:34 UTC

    Tie::File is probably the handiest way to do what you ask. Have you considered using something more than a flat file? The dbm interfaces that come with perl might serve. The advantage is that lookups are faster and you don't have to keep track of the physical layout in your program.

    After Compline,
    Zaxo

Re: Problem writting to a database (db)
by EdwardG (Vicar) on Oct 07, 2003 at 14:56 UTC
    Although I'm guessing at what kind of file a ".db" file is (Exim?), it looks like this question is answered in perlfaq5 - and I quote:
    How do I change one line in a file/delete a line in a file/insert a +line in the middle of a file/append to the beginning of a file? Use the Tie::File module, which is included in the standard distri +bution since Perl 5.8.0.
Re: Problem writting to a database (db)
by iburrell (Chaplain) on Oct 08, 2003 at 00:10 UTC
    The other posters have good suggestions about updating a data file. But if you want to learn about seek, read the documentation. The second argument is the offset, and the third argument tells what to measure the offset against. The possibilities are SEEK_SET (0), SEEK_CUR (1), and SEEK_END (2). You don't really need to seek to the beginning because that is where the filehandle starts.

    The end of the file is: seek($fh, 0, 2);

    The beginning is: seek($fh, 0, 0);

    The 10th byte from the beginning is: seek($fh, 10, 0);

    The 10th byte before the end is: seek($fh, -10, 2)