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

How do I globalise a variable so that when it is changed in one child it affects all children when using fork?
Or does it do that already in which case I am on the wrong track :)

Replies are listed 'Best First'.
Re: Global variables over many children
by Zaxo (Archbishop) on Dec 05, 2001 at 07:19 UTC

    Child processes get copies of the variables and environment. Changes in the child are local to the child. What you want is some form of shared memory. Check the IPC namespace and Mmap.

    Update: Re your reply, indeed OTT for what you want. A pipe, perhaps with a signal handler to read and close it, will do for that.

    After Compline,
    Zaxo

      Oh dear, It seems a bit OTT as im only trying to share a pid of a deeper forked child in a daemon.
      Anyway, I tried IPC::Sharable and got:
      IPC::Shareable::SharedMem: shmget: No such file or directory at /usr/lib/perl5/site_perl/5.6.0/IPC/Shareable.pm line 566 Could not create shared memory segment: at ./duke.pl line 8
      (Linux RH 7.2)
      Thanks for your help
        Don't use that. Use MLDBM::Sync, or just use a file if that seems like overkill.