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

Try doing the tie before you modify the hash. Also, check the MLDBM man page for examples.
  • Comment on Re: Re: Re: RE: Global variables over many children

Replies are listed 'Best First'.
Re^5: Global variables over many children
by dragoncoll (Initiate) on Oct 10, 2007 at 20:49 UTC
    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.