Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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.......

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 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 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
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 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

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?
[Cosmic37]: thank you Lotus I understand
[Cosmic37]: now I have tried another blunder - can anyone explain why I am such a dunderhead?
[Cosmic37]: if ( $line =~ /$mydt/ ) { print $line; }
[Cosmic37]: I try to match successive date times stored in variable $mydt
[Cosmic37]: I guess it is searching for the string "$mydt"
[Corion]: Indeed cool, erix ;)
[Cosmic37]: rather than the value of $mydt which is a date time strong such as 2016-01-01 12:30:56
[Corion]: Cosmic37: No, but maybe $mydt doesn't contain what you think it does, or it contains characters that are special in a regular expression? Try if( $line =~ /\Q$mydt\E/) { ... for a literal match
[Cosmic37]: I mean string grrr
[Corion]: Maybe add an else branch in which you print what the values of $line and $mydt are?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-06-29 16:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (673 votes). Check out past polls.