Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Perl Script Calling Another Perl Script

by mcogan1966 (Monk)
on Nov 20, 2003 at 20:07 UTC ( [id://308675]=perlquestion: print w/replies, xml ) Need Help??

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

I know this has probably been answered here before, but I'm going to ask it here anyway.

I have a Perl program (not a CGI script) that needs to call another program. This is not like the one calling many issue I had before. This is a simple one calling one once plan. The second program is going to genereate output that will go back to the first one. I can do this by piping, or I can make copies of this second program and put the relevant code in each Perl program that is going to use it, but both seem like the long way about doing this.

The basic concept is something like this:

In program one, there will be a line like:

$gimme = #some call to the second program;
It should be noted here that the call will also pass 2 parameters, in the form of variables, $var1 and $var2.

and in program two:

#!usr/local/bin/perl $output = "blah"; #actually generated in the program return $output; #...or something I don't know if return # is the right statement here either.
I'm sure there has got to be a simple solution to this. I'm just not foo enough to have it down in my brain yet.

Replies are listed 'Best First'.
Re: Perl Script Calling Another Perl Script
by jeffa (Bishop) on Nov 20, 2003 at 20:10 UTC
    Here is one way to skin this cat:
    # program one (one.pl) my $gimme = system('./two.pl','foo','bar'); # program two (two.pl) print join(':',@ARGV),"\n";
    Program two needs to print to STDOUT, not return.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Yup, as if the universe decided to prank on me (like when is it not), the answer basically fell into my lap just minutes after posting this.

      In program one:

      $out = `perl program2.pl "$var1" "$var2"`;
      And in program two:
      $output = "blah"; print $output;
      And of course, it all works just fine for me.

      *returns to banging head on desk

(z) Re: Perl Script Calling Another Perl Script
by zigdon (Deacon) on Nov 20, 2003 at 20:25 UTC

    Another option, that is probably more efficient, is to take the code from prog2, and move it into a module, that can then be used from many programs:

    You would create Code.pm:
    sub GenOutput { my @inputs = @_; my $output = "blah"; return $output; } 1;
    Then, prog1 can do:
    use Code; ... $gimme = &GenOutput($var1, $var2);
    And prog2 is:
    #!/usr/bin/perl use Code; &GenOutput;
    Does that make sense?

    -- zigdon

Re: Perl Script Calling Another Perl Script
by Roy Johnson (Monsignor) on Nov 20, 2003 at 20:38 UTC
    You might want to look into open(FH, '-|'). Then you could require program2 in the child block, and it would have access to all the variables, in addition to being able to write to the parent via STDOUT.

    Alternatively, you might want something like

    $gimme = `perl prog2 $var1 $var2`
    where prog2 will print (to STDOUT) whatever you want $gimme to get back from it.
      Yeah, that is the answer that basically fell on my head just minutes after making my post. Though my variant encloses $var1 and $var2 in double quotes. The reason for that is if either of them contain any spaces, the backtick will parse out the spaces as a break between arguments being passed, and I don't want that to happen.
Re: Perl Script Calling Another Perl Script
by shockme (Chaplain) on Nov 20, 2003 at 22:58 UTC
    I've recently started using Proc::Reliable for these situations. It's worked out quite well for me.

    If things get any worse, I'll have to ask you to stop helping me.

Re: Perl Script Calling Another Perl Script
by ysth (Canon) on Nov 21, 2003 at 00:12 UTC
    If you want the second script to directly get/set variables in the first script, you can say do "filename". See perldoc -f do. (That won't process any flags on the #! line, though.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://308675]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found