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

I have a small doubt in Perl? can any one do your favour? I have 4 Perl scripts like step1.pl, step2.pl, step3.pl, step4.pl.. I want to execute those 4 scripts consecutively such that the master script should exit if there is any error in any of the script...for example if there is an error in step1.pl, then it should not execute step2.pl, it should exit from the script.. if any of you had any idea how to do this, please help me.. I am a beginner of Perl.. please do needful.

Replies are listed 'Best First'.
Re: how to execute multiple Perl scripts
by eyepopslikeamosquito (Archbishop) on Dec 28, 2014 at 01:30 UTC

    Hello vasuperl and welcome to the Monastery!

    Since you are very new to Perl, you could get started and gain confidence by writing four small standalone test scripts (step1.pl, step2.pl, step3.pl, step4.pl) like so:

    # step1.pl print "This is step1.\n"; exit 0; # step2.pl print "This is step2.\n"; exit 0; # step3.pl print "This is step3.\n"; exit 42; # step4.pl print "This is step4.\n"; exit 0;
    We deliberately make these test scripts as simple as possible to make it easier to test. Note that we use the Perl exit function to explicitly set the script exit code. We use the standard convention that an exit code of zero indicates success, while non-zero indicates failure. We will test this exit code in our "runner" script to determine if any of the steps failed.

    With that preliminary step done, you can then edit these scripts in different ways to test different scenarios, and so confirm that your "runner" script works in all scenarios. For example, changing step3.pl above from exit 42 to exit 0 tests the case of all scripts successful.

    With that done, write runner.pl to run these four scripts as per your spec. For example:

    # runner.pl use strict; use warnings; $| = 1; my @scripts = ('step1.pl', 'step2.pl', 'step3.pl', 'step4.pl'); for my $scr (@scripts) { my $cmd = "$^X $scr"; print "Run '$cmd'..."; my $out = qx{$cmd}; my $rc = $? >> 8; print "rc=$rc\n"; print "Output:$out\n"; if ($rc != 0) { print "Main script $0 exit $rc\n"; exit $rc; } } print "All commands finished successfully.\n";
    Setting the $| = 1 perl special variable ensures that stdout is auto-flushed; this is commonly used in scripts that run external commands to make it clearer whether the stdout is coming from the runner script itself or the external command. The $^X special variable contains the name of the currently running perl, while $? is used to extract the exit code of the last command run, and $0 is the name of the current script (runner.pl in this case). To learn more about Perl special variables, see perlvar.

    The qx operator is used to run a command and capture its stdout. If you don't want to capture the command's output, simply use the system function instead.

    Please note that the example runner.pl above is just one way to do it, so as to get you started. Perl offers a wide variety of ways to run external commands depending on your needs. To learn more, see system, qx, exec and open -- or a Perl tutorial on the subject, for example perlhowto or How to run a shell script from a Perl program? (Perl Monks categorized answer).

    The advantage of starting with a small self-contained test script, as shown above, is that you can experiment with different ways to run an external command, and so improve your Perl.

Re: how to execute multiple Perl scripts
by trippledubs (Deacon) on Dec 27, 2014 at 19:32 UTC
    ./step1.pl && ./step2.pl && ./step3.pl && ./step4.pl
    If you are running Linux you can use the control operator && which executes the right command if the left command exited successfully (exit code 0). In your scripts ensure that scripts fail with non-0 exit codes so they will break the command list. Should work in Windows also judging from a quick search.
      For completeness these are shell commands not perl code.

      If "master script" means Perl, then the OP should have a look at exit and system or qx or do (depending on what he wants)

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: how to execute multiple Perl scripts
by pme (Monsignor) on Dec 27, 2014 at 19:53 UTC
    You can do it with shell:
    #!/bin/sh set -e ./step1.pl ./step2.pl ./step3.pl ./step4.pl
    Of course the exit value of the perl scripts must be set correctly.
Re: how to execute multiple Perl scripts
by Anonymous Monk on Dec 28, 2014 at 01:08 UTC
    my @scripts = qw/step1.pl step2.pl step3.pl step4.pl/; for my $script (@scripts) { system('perl',$script)==0 or die "failed to execute $script: \$?=$?"; }