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

I wrote a script that runs several "subscripts", using the 'system' command. I'd like a subscript to be able to declare and communicate a variable to my master script. For example, if "event1" occurs during the subscript, tell the master script that it did occur. I tried using the "our" variable declaration but had no luck (I've successfully used it elsewhere though). I'm sure this is a very elementary thing, but I'm having trouble finding the correct way to do it.

Replies are listed 'Best First'.
Re: Communication between scripts
by SuicideJunkie (Vicar) on Dec 17, 2012 at 21:34 UTC

    Peruse perlipc and take a look to see what applies best to you. Pipes, sockets, signals, or perhaps just print to STDOUT and capture it with backticks.

Re: Communication between scripts
by kennethk (Abbot) on Dec 17, 2012 at 22:13 UTC

    Also common would be to centralize and use modules instead of cascading scripts.

    If you are set on multiple scripts, you can use globals (our) combined with do (or require) rather than system. But this is probably not a good idea for new code, since this gets messy fast, and the execution environments can have odd effects on each other.

    #!/usr/bin/perl -w use strict; our $x = 1; require 'script2.pl'; print $x;
    #!/usr/bin/perl -w use strict; our $x = 2;

    SuicideJunkie's suggestions are where you should look first (maybe even w/ a fork and exec); however, if you are dealing with legacy, this might help.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.