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

Here it is....
sysopen(DB_USERS_LOCK, $lck_users, O_RDWR|O_CREAT) || die( "Cannot open $lck_users: $! " ); flock(DB_USERS_LOCK, LOCK_EX) || die( "Cannot LOCK_EX $lck_users: $! " ); tie %h, "DB_File", $users_dbm_file, O_RDWR|O_CREAT, 0640, $DB_HASH || die( "Cannot tie $users_dbm_file $! \n" ); my $data_for_db = ""; foreach ( sort keys %data ){ $data_for_db = $data_for_db . "$_ $data{$_} " } $h{$username} = $data_for_db; print $h{$username}; untie %h; close DB_USERS_LOCK;
i am tring to dump the %data hash into a scalar value so i can add it to the DB_HASH.... $username has been predefined earlier in the script. when i check the file afterwords the file size is still 0. any suggestions? kha0z -- www.kha0z.net

Replies are listed 'Best First'.
Re: Add hash to DB_HASH file using DB_File
by dws (Chancellor) on Apr 09, 2001 at 09:55 UTC
    It looks like you're getting tripped up by operator precendence, and are misdiagnosing (by failing to correctly diagnose) a tie failure.
    tie %h,"DB_File",$users_dbm_file,O_RDWR|O_CREAT,0640,$DB_HASH || die( "Cannot tie $users_dbm_file $! \n" );
    isn't doing what you expect. Unless $DB_HASH is 0, die will never get invoked. And if $DB_HASH is 0, die will be invoked, but not in the way you expect.

    Ponder for a moment the different subtle between || and or. You might wish to read the SYNOPSIS in perlop.

      I probably overuse them a bit but if there is the smallest doubt I bracket my functions. Looking at that little knot reminds me why its a good idea :)

      --
      my $chainsaw = 'Perl';

        Bracketing your functions isn't quite the lesson I would expect you to draw from this, though it works. More flexible, in my opinion at least, is to use   somesub arg1, arg2, arg3 or die "message"; instead of   somesub arg1, arg2, arg3 || die "message"; which is really  somesub arg1, arg2, (arg3 || die "message");