Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

DB_File, not saving

by sulfericacid (Deacon)
on Jul 17, 2003 at 18:57 UTC ( #275336=perlquestion: print w/ replies, xml ) Need Help??
sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

First real attempt at a DB other than SDBM and came across a problem. Nothing is being saved to the database (it prints to screen one time then after you refresh or add something else it's gone).

This is a real short snippet of the code I'm using, anyone have suggestions?

use strict; use warnings; use POSIX; use CGI qw(:standard start_table end_table); use lib ""; use Tie::IxHash; my $columns = 50; use Text::Wrap qw( wrap $columns ); use DB_File; my %chat; my %chatorder; my @words = (); my $chat = "chat.dbm"; # location of database my $file = "count.txt"; # location of count file unlink "chat"; tie %chat, "DB_File", "chat", O_CREAT | O_RDWR, 0644, $DB_HASH or die "Cannot open file 'fruit': $!\n"; tie %chat, "Tie::IxHash" or die "Cannot tie %chat to Tie::IxHash : $!\n"; tie %chatorder, "Tie::IxHash" or die "Cannot tie %chatorder to Tie::IxHash : $!\n"; print header, start_html; my $num; foreach ( keys(%chat) ) { $num++; } print "DB keys: $num"; my $name = param('name'); my $message = param('message'); my $cnt; if (param) { if ($name) { if ($message) { my $keeptime = join ( '~', $hour, $min, $sec ); my $info = join ( '~~', $name, $message, $keeptime ); $chat{$cnt} = $info; } else { print "Message was missing, data not sent.<br>"; } } else { print "Name was missing, data not sent.<br>"; } } foreach ( reverse keys(%chat) ) { $add++; if ( $add <= 10 ) { $chatorder{$_} = $chat{$_}; } } foreach ( reverse keys(%chatorder) ) { my ( $name, $message, $time ) = split /~~/, $chatorder{$_}; $message = wrap( '', '', $message ); print Tr( td( { -width => '700' }, "<font color=blue>&lt;$name @ $time&gt;</font>$message" ) ),


"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

Comment on DB_File, not saving
Download Code
Re: DB_File, not saving
by sauoq (Abbot) on Jul 17, 2003 at 19:08 UTC

    After you tie %chat to DB_File you re-tie it to Tie::IxHash. (So, you never really use DB_File at all.)

    -sauoq
    "My two cents aren't worth a dime.";
    
      Then what do you suggest? I need to tie %chat to Tie::IxHash to retain insertion order and I need tie it to the DB_File or it's useless.

      "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
        Then what do you suggest?

        I suggest you use an array instead of a hash and tie to DB_file using the DB_RECNO file type.

        -sauoq
        "My two cents aren't worth a dime.";
        
      Commenting out unlink "chat"; would probably help too...

      Update: I meant in addition to sauoq's comment, not instead of.

        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.";
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://275336]
Approved by sauoq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-12-25 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls