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

how do i pass a string from a perl program to another calling program(also in perl)? (something like a return statment but between two programs instead of a subroutine.
  • Comment on passing a parameter to an outside program

Replies are listed 'Best First'.
Re: passing a parameter to an outside program
by ikegami (Patriarch) on Oct 05, 2005 at 20:51 UTC

    The child needs to print the data out to standard input (which the parent must have ready to capture) or to a temporary file (whose name the parent might have passed as an argument).

    The simple case where the parent waits for the child to complete can be written as:

    parent ------ $result = `child.pl`; child ----- print($result);
Re: passing a parameter to an outside program
by InfiniteSilence (Curate) on Oct 05, 2005 at 21:29 UTC
    There are many ways to do this. Here are a few not yet mentioned:

    envpass.pl:

    #!/usr/bin/perl -w use strict; #set environment variables $ENV{FOOBAR} = 10000; #Set package level variables package manic; our $vroom = 10097; #You can use Data::Dumper package main; use Data::Dumper; my $foobie = 19897; open(H,qq|>junk.dat|) or die $!; print H Data::Dumper->Dump([$foobie],[qw[foobie]]); close(H); 1;

    envpass2.pl

    #!/usr/bin/perl -w use strict; do 'envpass.pl'; print $ENV{FOOBAR}; print $manic::vroom; #read in that data dumper stuff my $stuff = `cat junk.dat`; no strict 'vars'; eval($stuff); print $foobie; 1;

    Celebrate Intellectual Diversity

Re: passing a parameter to an outside program
by Skeeve (Parson) on Oct 05, 2005 at 20:58 UTC
    If you need more complex parameter passing than what ikegami mentioned, you might like to look at perldoc perlipc

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: passing a parameter to an outside program
by Roy Johnson (Monsignor) on Oct 05, 2005 at 21:42 UTC
    That depends on how you're calling the other program. If you're using backticks, the called program can just print the string and the calling program will capture it.

    Caution: Contents may have been coded under pressure.
Re: passing a parameter to an outside program
by nedals (Deacon) on Oct 05, 2005 at 22:04 UTC
Re: passing a parameter to an outside program
by samizdat (Vicar) on Oct 06, 2005 at 15:02 UTC
    I'm not clear from your question whether you want to only communicate at the end of the child process or while both are running concurrently. If the former is truly what you want, then the answers about backticks et al are appropriate. Environment variables are also fairly easy, but you can only use them when the programs are spawned from the same shell. IPC in a more general sense, through sockets or shared memory, is much more complicated but also much more powerful.

    And, a caveat, all of the more powerful suggestions imply that you are on a *NIX system of one kind or another, not a Windows box.