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

I have a script as follows that I want to execute automatically:

perl -I -Mmod1 -Mmod2 pgm.pl paramFile perl -I -Mmod3 -Mmod4 pgm2.pl paramFile2

I think I should put this into a makefile and run it but don't know how? If makefile is not appropriate, any other solution?

Replies are listed 'Best First'.
Re: how to create makefile?
by Corion (Patriarch) on Jan 09, 2015 at 08:53 UTC

    Your question is not really a Perl question and more a question of job control or a shell question. Personally, I would use a plain shell script instead of a makefile, as you have not explained how the two processes depend on each other and by what mechanism make would determine that it only needs to run the second step.

    #!/bin/ksh set -e perl -I -Mmod1 -Mmod2 pgm.pl paramFile perl -I -Mmod3 -Mmod4 pgm2.pl paramFile2

    The -e switch makes the shell script stop if any of the programs fails.

      Perhaps, I have mixed up the c makefile with perl makefile. as we put all c programs in the make file, we compile and then, we run, I thought that I can run automatically the perl programs in the same manner.

      Regarding the dependency, the first script has to be finished without error as it generates files for the 2nd script. beyond that there is no other dependency.

      I should perhaps have said that all modules, mod1, ..., mod4 and pgm1 and pgm2 are in the same directory. If they are not, I think for the modules, the path should be given after -I.

        This sounds like a great job for make, but this is Perlmonks, not makemonks. I suggest that you learn about make and then use that knowledge to create the appropriate Makefile. If you want to implement this in Perl, there is Algorithm::Depends, which allows you to list file dependencies and then perform actions based on the dependencies.

        The Makefile for your case would likely be something like the following. You will need to preserve the tabs, because tabs are special characters in make:

        .PHONY all all: resultfile1 resultfile2 resultfile1: perl -I -Mmod1 -Mmod2 pgm.pl paramFile resultfile2: resultfile1 perl -I -Mmod3 -Mmod4 pgm2.pl paramFile2

        As you haven't told us the names of the resulting files of the program runs, I've substituted resultfile1 and resultfile2 for them.

Re: how to create makefile (why)?
by Anonymous Monk on Jan 09, 2015 at 08:39 UTC
    For what purpose?
      system
      ## mystuff.pl ## by carolw system $^X, qw/ -Mmod1 -Mmod2 pgm.pl paramFile /; system $^X, qw/ -Mmod3 -Mmod4 pgm2.pl paramFile2 /;