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

Hi,
I have a set of perl scripts added to a batch file, like one below the other. Somewhat like this:
test.bat

script1.pl
script2.pl
script3.pl

In case a script2.pl fails (say because of file not found or likewise), would script3 be executed?
If it is, then is there any way one can prevent it?
Would it be better to put the scripts in another perl script using system commands?
Which one would be better?
I tried system and then "or die" with it, though the perl script works fine (in system) , I get an error.

Thanks!

Replies are listed 'Best First'.
Re: batch query - win32
by BrowserUk (Patriarch) on Nov 04, 2003 at 05:36 UTC

    Script3.pl would be run if script2.pl died.

    To prevent it, use

    script1.pl script2.pl && script3.pl

    Script2 will always be run regardless of how script1 exits. Script3 will only be be run if script2 succeeds -- Ie. returns 0, which it won't if it dies or exits with any value other than 0.

    You can also do

    script1 || script2;

    In which case, script2 will only be run if script1 fails.

    P:\test>perl -e"exit 1" && echo succeeded || echo failed failed P:\test>perl -e"exit 0" && echo succeeded || echo failed succeeded P:\test>perl -e"die" && echo succeeded || echo failed Died at -e line 1. failed

    You can also group commands

    (prog1 && prog1) || (prog3 || prog4)

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

Re: batch query - win32
by etcshadow (Priest) on Nov 04, 2003 at 05:09 UTC
    Writing a simple wrapper script in perl would probably be easier than doing it in a DOS batch file. Also, it's gonna jive with something you already know.

    The thing about system is that it returns the exit value of the system command. System commands return 0 for success. So you really want to say system "blah" and die as opposed to system "blah" or die.


    ------------
    :Wq
    Not an editor command: Wq
      Win32::Process might work better than system()
      system "blah"and die ...

      Yes. Exactly.

Re: batch query - win32
by inman (Curate) on Nov 04, 2003 at 09:52 UTC
    In your command script you will need to test the value of errorlevel. This is the error value of the last command that ran. If you execute a perl script that exists then errorlevel will be set to 0 (indicating success) unless you used the exit function (or similar) to set the exit status from perl. If the script cannot be found, then Perl itself will set the errorlevel to an error value.

    The following code examples may be of use.

    retcode.pl

    #! /usr/bin/perl -w # retcode.pl # Sets the errorlevel passed as first argument use strict; my $retcode = $ARGV[0] || 0; print "Return code = $retcode\n"; exit $retcode;

    returncode.cmd

    @echo off rem A Command file that uses conditional processing. echo Executing First Script retcode.pl 0 if errorlevel 1 goto end_label echo Executing Second Script retcode.pl 0 :end_label echo Done!

    Play around with the return codes sent to the script and the name of the first scrip to see that the script will jump to the label if the script returns an error value greater than 1 or the script itself is not found.

    inman

Re: batch query - win32
by vek (Prior) on Nov 05, 2003 at 00:00 UTC

    Something I've done in the past (admittedly for cron jobs but could still be applied here) is have each script set a fail flag (a zero byte size file for example) that would tell the next script to exit without running.

    In script2.pl:

    if ($you_do_not_want_script3_to_run) { # create zero byte size file... open(FAILFLAG, ">/tmp/script2.failflag") || do { # handle the error here }; close (FAILFLAG); }

    Then in script3.pl:

    my $script2_failflag = "/tmp/script2.failflag"; if (-e $script2_failflag) { # script3.pl should not contine any further exit(); }
    -- vek --
      Why on earth wouldn't you just check the exit status of the scripts you're executing?

      ------------
      :Wq
      Not an editor command: Wq

        Well, as I mentioned, I have used fail flags for cron jobs in the past (rules out checking the exit status) and just thought I'd bring it up as something else the OP could try if so inclined. You know, TIMTOWTDI and all that :-)

        -- vek --
Re: batch query - win32
by Anonymous Monk on Nov 04, 2003 at 05:48 UTC

    Out of words actually!!!!
    Can think of THANKS though!