Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Automatically running series of Perl programs

by neversaint (Deacon)
on Mar 09, 2006 at 14:51 UTC ( #535398=perlquestion: print w/ replies, xml ) Need Help??
neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,
I have 3 Perl codes, let's call them: foo.pl, bar.pl, tins.pl. All of them would run sequentially starting from foo.pl. So normally I would do this three lines manually, waiting for each code to finish to get to the final result.
perl foo.pl param1 > output1.txt perl bar.pl output1.txt > output2.txt perl tins.pl output2.txt > final_result.txt
My question is, how can one writes a single Perl script that would run those three codes automatically?

---
neversaint and everlastingly indebted.......

Comment on Automatically running series of Perl programs
Download Code
Re: Automatically running series of Perl programs
by philcrow (Priest) on Mar 09, 2006 at 14:58 UTC
    There are several ways including using backticks `` around each command. Most people probably prefer using the system command. You could also open each program on a file handle:
    open PROG "perl foo.pl param1 | " or die "...";
    Then you can handle the io.

    You could also simplify your three steps into one and write a shell script (did I really suggest that?):

    perl foo.perl param1 | perl bar.pl | perl tins.pl > result.txt

    Phil

      Yeah, I think you really did suggest that. Furthermore, I'd suggest a shell script, too. I usually use Perl scripts rather than shell scripts even when a shell script would do quite well enough, but this really strikes me as something simple enough that using Perl is complete overkill and a waste of time, unless running the three other scripts in sequence is part of a larger algorithm somehow.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

Re: Automatically running series of Perl programs
by SamCG (Hermit) on Mar 09, 2006 at 14:59 UTC
    Does:
    system ("perl foo.pl param1 > output1.txt"); system ("perl bar.pl output1.txt > output2.txt"); system ("perl tins.pl output2.txt > final_result.txt");
    not work?
Re: Automatically running series of Perl programs
by smokemachine (Hermit) on Mar 09, 2006 at 15:58 UTC
    can't you use something like this?
    perl foo.pl param1 | perl bar.pl | perl tins.pl > final_result.txt
    like:
    perl -e 'print "test"' | perl -ne 'print "--$_"' | perl -ne 'print "$_ +-- \n"' > final_result.txt
Re: Automatically running series of Perl programs
by duff (Vicar) on Mar 09, 2006 at 16:13 UTC

    Another option in the TMTOWTDI department is to just stick those lines in a shell script (if on a unixish OS) or a batch file (if on a windowsish OS) or whatever is appropriate for you OS.

Re: Automatically running series of Perl programs
by radiantmatrix (Parson) on Mar 09, 2006 at 17:12 UTC

    Well, this is probably easier done as a shell script, but since you asked:

    use strict; use warnings; my %ops = ( 'foo' => ['param1','output1.txt'], 'bar' => ['output1.txt','output2.txt'], 'tins' => ['output2.txt','final_result.txt'], ); foreach (qw[foo bar tins]) { my $outfile = pop @{ $ops{$_} }; system( $^X, $_.'.pl', @{ $ops{$_} }, '>', $outfile); # $^X is per +l interp. }
    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-10-24 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls