in reply to Re: RE: Global variables over many children
in thread Global variables over many children

Im having a lot of trouble getting my head round this, sorry to be a nuisance. Ive tried the following:
#!/usr/bin/perl -w use MLDBM::Sync; use Fcntl qw(:DEFAULT); $tied_hash{list} = [1,2,3,4,5]; #my $sync_dbm_obj = tie %tied_hash, 'MLDBM::Sync', '/tmp/mldbmsync', O +_CREAT|O_RDWR, 0640; push @{$tied_hash{list}},6; @values = @{$tied_hash{list}}; print @values;
which prints 123456 as you would expect, but as soon as I uncomment the tie, nothing is printed.
TIA

Replies are listed 'Best First'.
Re: Re: Re: RE: Global variables over many children
by blakem (Monsignor) on Dec 07, 2001 at 12:55 UTC
    Just a guess... try this: (untested)
    $tied_hash{list} = [1,2,3,4,5]; my $sync_dbm_obj = tie %tied_hash, 'MLDBM::Sync', '/tmp/mldbmsync', O_ +CREAT|O_RDWR, 0640; # modify list my $listref = $tied_hash{list}; push(@$listref,6); # store list back into hash. $tied_hash{list} = $listref; # did it work? @values = @{$tied_hash{list}}; print @values;

    -Blake

Re: Re: Re: RE: Global variables over many children
by perrin (Chancellor) on Dec 07, 2001 at 20:47 UTC
    Try doing the tie before you modify the hash. Also, check the MLDBM man page for examples.
      Hi guys,
      I trying using the solution showed in above, but I created four MLDBM objects.
      $dataToConnectId = tie %dataToConnect ,'MLDBM::Sync', '.shared1 +', O_CREAT|O_RDWR, 0640; $dataFromConnectId = tie %dataFromConnect,'MLDBM::Sync', '.shared2 +', O_CREAT|O_RDWR, 0640;

      after this, I created some functions to read this guys:
      sub getDataFromConnect { return; my $line; $dataFromConnectId->Lock(); my $ref = $dataFromConnect{list}; if (@$ref) { $line = shift @$ref; $dataFromConnect{list} = $ref; print "entrou 5->$line<-\n"; } else { $line = ""; } $dataFromConnectId->UnLock(); return $line; } sub getDataToConnect { my $line; $dataToConnectId->Lock(); my $ref = $dataToConnect{list}; if (@$ref) { $line = shift @$ref; $dataToConnect{list} = $ref; printf "entrou 6\n"; } else { $line = ""; } $dataToConnectId->UnLock(); return $line; } sub setDataToConnect { $dataToConnectId->Lock(); my $ref = $dataToConnect{list}; push @$ref, @_[1]; $dataToConnect{list} = $ref; $dataToConnectId->UnLock(); print "entrou 3\n"; } sub setDataFromConnect { $dataFromConnectId->Lock(); my $ref = $dataFromConnect{list}; push @$ref, @_[1]; $dataFromConnect{list} = $ref; $dataFromConnectId->UnLock(); print "entrou 4\n"; }

      This functions are used by TWO different proccess. BOTH are running in the same program, but I used the FORK command to create another one.
      The problem is that when I write in the array @dataToConnect, the function that should read it, don't read anything and the other function used to read the @dataFromConnect reads this guy! It's very strange!

      Please could you take a look what I made wrong?

      thanks a Lot!
      Joćo

        This can't be your code. Your getDataFromConnect() sub starts with a return(). The problem you describe sounds like a scoping problem. Maybe $dataFromConnect and $dataToConnect are not what you think they are.

        You don't need any of those Lock/Unlock calls. MLDBM::Sync does this for you.