Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Passing Data Between Perl Programs

by jmmitc06 (Beadle)
on Jul 03, 2013 at 14:38 UTC ( #1042228=perlquestion: print w/replies, xml ) Need Help??
jmmitc06 has asked for the wisdom of the Perl Monks concerning the following question:

So I've been trying to figure out a good way to pass the contents of a variable between two concurrently running perl programs. So for example, program 1 generates a variable '$number' and I want it to be sent to a second program to do something with (e.g. program 2 would print $number). I would like to do this without writing a temporary file or actually writing the contents of the variable somewhere by program 1 and reading it back into program 2, I want to move the data as directly (poor word choice, but hopefully it captures what I am trying to do) as possible between the two programs.

Also, does a similar method exist for passing data directly between a perl program and python or vice versa?

Thanks for any help.

Replies are listed 'Best First'.
Re: Passing Data Between Perl Programs
by choroba (Chancellor) on Jul 03, 2013 at 14:46 UTC
Re: Passing Data Between Perl Programs
by Preceptor (Deacon) on Jul 03, 2013 at 18:26 UTC

    It's a very broad question - inter process communication is potentially very complicated, and you've a variety of methods and approaches. Just remember - synchronizing two processes to pass data is 'expensive' in terms of processor time, so it's better if you can think of a way to do it asynchronously. But you've also got to be careful to watch for locking between the two. So the answer is always 'it depends'.

    In addition to what's already been mentioned - you might want to consider running threads, and using thread-level data transfer. threads::shared, Thread::Queue, Thread::Semaphore are possibilities.

      Thanks for the reply, I actually use Threads, especially Queue alot here at work but I have never messed with IPC. It's probably not practical to actually share data between two processes for my work but I was curious. I haven't played much with Semaphore though, will have a look at that.

Re: Passing Data Between Perl Programs
by kcott (Canon) on Jul 04, 2013 at 08:55 UTC

    G'day jmmitc06,

    Is this the type of thing you had in mind?

    "program 1 generates a variable '$number'"

    $ cat #!/usr/bin/env perl while (<>) { chomp; my $number = length; print "$number\n"; }

    "program 2 would print $number"

    $ cat #!/usr/bin/env perl while (<>) { print "Got $_" }

    Sample run ( reads user input from which it generates a number which it passes to where the number is included in an output message):

    $ | 1 12 123 1234 12345 Got 1 Got 2 Got 3 Got 4 Got 5

    -- Ken

      Not exactly but thanks, I know about piping I'm wanting to have two programs running concurrently, program 1 generating data and passing it to the already running program 2 where something is done to it. The inter process communication module mentioned above does what I was looking for. Thanks though

        If it's a linear feed, you can probably use a FIFO pipe (assuming Unix, I don't know how easy it is on Windows). man mkfifo. Basically it's a file that exists purely as a place for ... well, once process to feed into and another to read out of. You use it by (once making it) by simply having one process open it for writing, and the other for reading.

Re: Passing Data Between Perl Programs
by Anonymous Monk on Jul 04, 2013 at 01:24 UTC

    It might be best to look into piping, thats seems to be what you are looking for.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042228]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-03-30 21:48 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (364 votes). Check out past polls.