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

One thing I have always liked about the idea of perl threads is that variables can be shared between the threads, allowing for eaiser control of their actions. My system, however, does not support threading, so I was wondering if anyone had any idea of how to implement this kind of thing with real processes and forks. I would like to avoid using unix domain sockets, but I don't know if that can be avoided...

Thanks in advance,
Jules Kongslie
xeh007@yahoo.com

Replies are listed 'Best First'.
Re: Variable Sharing
by btrott (Parson) on Jun 22, 2000 at 00:29 UTC
    You may have to implement the sharing method yourself. In other words, if you want to share a variable between two different processes, you may have to implement some method of serializing that variable; then you can store the serialized version in a file or in a database and both processes can have access to it. Managing changes to the variable across the separate processes will be a challenge.

    For serializing the data, look into Storable or Data::Dumper.

    Perhaps you could use shared memory for this; you might want to check out IPC::SharedCache, which lets you store data in shared memory. It automatically serializes the data for you. Actually, I'd say this is a good place to start looking.

Re: Variable Sharing
by lhoward (Vicar) on Jun 22, 2000 at 00:33 UTC
    Check out IPC::Shareable. If you are on unix it allow you to share variables between processes on the same system.
Re: Variable Sharing
by eduardo (Curate) on Jun 22, 2000 at 19:43 UTC
    hm... depending on your system, this may or may not be a trivial thing to implement. now, i am working on old memories i don't really have any more, but if i remember correctly, a lot of modern OS's will have variable sharing via shmget and shmput and other such LOW level system calls, however depending on the cleaness of the semaphore calls, and the guarantee of atomicity of update, this may become a very difficult thing. We've had very limited success with the IPC shared cache and IPC sharable modules from CPAN, because they do a serialization of data structures, which if you are in a speed quasi-critical enviroment, it becomes a cost prohibitive operation... again, if you must have shared variables (and make sure you understand why you have to have them... they may not be neccessary, and it may be easier to retain state in some other daemon which you poll via a pipe or something along those lines...) the CPAN modules will really help you out.