in reply to How to pass the value of a variable from a child to parent with gui interface?

or what I might be doing terribly wrong?

Processes do not share variables. Once you fork(), each process has its own copy of the variables, and any changes to the variables are local to the process. The subject you need to research is IPC (inter process communication). You might start by reading perlipc.

Here is an example of what you can do:

use strict; use warnings; use 5.010; my @lines = qw{ LINE1 LINE2 LINE3 LINE4 LINE5 }; my $pid = open my $INPUT_FROM_CHILD, '-|'; if (!defined $pid) { die "Couldn't fork: $!"; } if ($pid) { say 'parent: blocking until child sends some data...'; while (<$INPUT_FROM_CHILD>) { #line oriented reading chomp; say "parent: $_"; } waitpid($pid, 0); say "parent: child exited with status = $?"; say 'parent: exiting...'; } else { sleep 2; say $lines[int(rand(@lines))]; close $INPUT_FROM_CHILD; sleep 10; }
Note the consequences to the parent, which is not something you want to do in a gui setting. The gui environments I'm familiar with--perl not being one of them--usually have a special setup for executing long running processes, which automatically signal the gui when they are done. You can then set up a listener for that 'event' and do something in response.
  • Comment on Re: How to pass the value of a variable from a child to parent with gui interface?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to pass the value of a variable from a child to parent with gui interface?
by BrowserUk (Patriarch) on Mar 13, 2010 at 04:38 UTC
    Processes do not share variables. Once you fork(), each process has its own copy of the variables,

    Under win32, fork is just threads::create() in disguise.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      ...which also does not share variables. Each thread has its own copy of the variables.

      forks::shared and threads::shared does allow sharing.

        So, just add threads::shared and you can shared variables perfectly well. Just as I've already suggested to the OP.

        The quality of your responses has definitly taking a serious nose-dive over the past couple of weeks or so. It reminds me of Jenson Button last season. Just when he looked to have his goal with in his grasp, he let the yips get to him.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: How to pass the value of a variable from a child to parent with gui interface?
by ZJ.Mike.2009 (Scribe) on Mar 13, 2010 at 04:39 UTC

    @7stud, thanks for pointing this out: "Processes do not share variables". I think this is the crux of my problem. I'll take a look at perlipc. Thanks :)

    BTW, I'm running ActivePerl on Windows box, your illustration code has this "open my $INPUT_FROM_CHILD, '-|';", which does not work for me. But thanks the same :)