Perl: How to update value of global variable inside a function (dynamically) when global variable is updated outside the function

Viewed 56

Code Snippet:

   my $timeExceedPerProduct;
    if ($pid= fork()) {
        &func1;
    }
    else (defined ($pid)) {
        sleep 100;
        $timeExceedPerProduct=1;
        sleep 20;
    }
    
    sub func1 {
         foreach (1..10) {
             sleep 30;
             if ($timeExceedPerProduct) {
                 exit 1;
             }
         }
    }

Here, I want that as soon as $timeExceedPerProduct gets updated in the child process of fork, in the next iteration of foreach loop (inside func1), $timeExceedPerProduct value used for verification (inside if) should be updated one.

How can I achieve this?

Currently, $timeExceedPerProduct used inside foreach loop in subroutine is the initial value

0 Answers
Related