Hi Frank

See in-line.

"Frank Naude" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Hi,
>
> Can somebody please help me to get this code fixed? Since I've moved the
> DB_File "tie" and "untie" functions to sub's, data is never written to the
> database. I've tried both call-by-refeence and call-by-value methods
without
> any luck.
>
> use strict;
> use DB_File;
> sub openDB {
>   my %db;
>   tie(%db,'DB_File',"db",O_CREAT|O_RDWR,0666);
>   return \%db;
> }

OK so far.

> sub closeDB {
>    # my $parm = shift;
>    my ($parm) = @_;
>    my %db = %$parm;
>    untie(%db);
> }

%$parm may be tied (use 'if (tied %$parm)' to make sure), but %db isn't. All
you've done is copied the values from the possibly tied hash to the
definitely untied hash and then tried to untie an untied hash. (Whether the
module actually lets you copy the entire database into a local hash is
another matter.) How about:

    sub closeDB {
        my $parm = shift;
        untie %$parm if tied %$parm;
    }

> # Write data to DB...
> my $z1 = openDB;

OK

> my %x1 = %$z1;

Legal but, again, copies the entire hash value to a new untied hash.

> $x1{'A'} = "B";     # PROBLEM IS THIS DATA WILL NEVER BE WRITTEN TO THE DB
> $x1{'B'} = "C";
> $x1{'C'} = "D";

No. %x1, again, is untied - it is just a normal hash. Try:

    $z1->{'A'} = "B";     # NO PROBLEM THIS DATA WILL ALWAYS BE WRITTEN TO
THE DB
    $z1->{'B'} = "C";
    $z1->{'C'} = "D";


> print "A = $x1{'A'}\n";
> closeDB(\%x1);
>

Well, I think you know what's wrong now. CloseDB doesn't stand a chance of
working. You're passing it a reference to plain, untied hash to untie; this
it tries to do by copying this untied hash to another untied hash (see
above) and untying /that/ :)

>
> # Read data back...
> my $z2 = openDB;
> my %x2 = %$z2;
> print "A = $x2{'A'}\n";
> closeDB(\%x2);
> # EOF
>

'Nuff said.

>
> Best regards.
>

Cheers Frank, and a happy New Year.


Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to