in reply to Re: Re: DB_File, not saving
in thread DB_File, not saving

He's just doing that once at the beginning of the script to remove his old file before writing a new one. That shouldn't matter (as long as he wants to start fresh each time the script runs.)

Update: jsprat is correct. The file should not be unlinked. (It is obvious, on closer inspection, that the script is useless without it.)

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Re: DB_File, not saving
by jsprat (Curate) on Jul 17, 2003 at 20:00 UTC
    I don't think he wants to start fresh each time. He wants to save the data so he can display more than one line.

    It looks like a CGI script that wants to accept a line of chat, then display the previous 10 lines. What it actually does is this:

    1. deletes all previous messages
    2. creates the db file, ties %chat
    3. unties %chat, thereby not storing any messages
    4. accepts a new message
    5. displays the last 10 messages (which were just deleted!)

    Not only is the %chat untied from the the DB_File when it's tied to TIE::IxHash (as you pointed out), the script is deleting the DB_File every time it is hit.

      You are right; he shouldn't delete it.

      -sauoq
      "My two cents aren't worth a dime.";
      
      You're right. This is a chat script (much like the chatterbox). It takes the latest 10 messages and prints to screen and it stores the rest for logs later on. I removed the unlink (no idea what it does/did but it was in the example I used) and the problem is still here (you probably knew that). I must be able to tie %chat to both objects otherwise it's useless, I'll look into using it as an array.

      Thank you for your help!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        I must be able to tie %chat to both objects otherwise it's useless

        Pretty strong words, partner ;-) I think what you mean is that to pull this off you must be able to represent the data in a sorted manner.

        First off, sauoq was right on about IxHash.

        Second, diotalevi says to use $DB_BTREE. An excellent suggestion, as long as you use a sensible key - maybe date/time stamp - and be aware of the possibility of duplicates. It will always be sorted - it's stored in a balanced tree. The key your script uses is $cnt, but I don't see it being initialized anywhere (probably not a sensible key ;). Every message is stuffed into the same slot, overwriting the previous message.

        The most straightforward short-term solution for you IMO would be to use $DB_RECNO like so:

        # tie your array to the file tie @chat, 'DB_File', 'chat', O_CREAT | O_RDWR, 0644, $DB_RECNO; # push the message info onto the array push @chat, "Message number $_" for (1 ..20); # print the last five messages print join "\n", @chat[-5 .. -1]; __END__ output: Message number 16 Message number 17 Message number 18 Message number 19 Message number 20

        You'll be able to treat @chat like an ordinary array. As a bonus, you'll be able to view or modify the chat db file with any text editor.

        No, its not useless. Instead of $DB_HASH which has no order, use $DB_BTREE which is sorted and eschew Tie::IxHash.