Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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:,, All of them would run sequentially starting from So normally I would do this three lines manually, waiting for each code to finish to get to the final result.
perl param1 > output1.txt perl output1.txt > output2.txt perl 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.......

Replies are listed 'Best First'.
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 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 | perl > result.txt


      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
    system ("perl param1 > output1.txt"); system ("perl output1.txt > output2.txt"); system ("perl output2.txt > final_result.txt");
    not work?
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. }
    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
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 smokemachine (Hermit) on Mar 09, 2006 at 15:58 UTC
    can't you use something like this?
    perl param1 | perl | perl > final_result.txt
    perl -e 'print "test"' | perl -ne 'print "--$_"' | perl -ne 'print "$_ +-- \n"' > final_result.txt

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://535398]
Approved by Corion
Discipulus de gustibus non disputandum
[LanX]: tasty bus ? ; -p

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-04-28 11:30 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (521 votes). Check out past polls.