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

Hi all

I need to know how I can execute multiple scripts from a single script (called master script). Code I created is like below
$generatorDir = "./processing" @rprtname = ("1","2","3","4","5","6","7","8","9",) my $rpcount=0; foreach(@rprtname) { my $rptscript = $rprtname[$rpcount]; `perl $generatorDir/$rptscript.pl` ; $rpcount++ }


Is this code right. Can anyone suggest me any better option.

Replies are listed 'Best First'.
Re: Executing multiple perl scripts by master script.
by toolic (Bishop) on Jan 23, 2010 at 19:27 UTC
    Aside from a couple of syntax errors, the code is "right" if it does what you want it to do. Here is a functionally equivalent re-write:
    use strict; use warnings; my $generatorDir = './processing'; my @rprtname = 1 .. 9; for (@rprtname) { `perl $generatorDir/$_.pl`; }
    Your code will execute 9 scripts in sequence. If your code does not do what you want it to do, please explain how it differs from your expected behavior.

    An enhancement would be to use system instead of backticks, and check the status of each command.

    system "perl $generatorDir/$_.pl" and die "Error running $_.pl $?" +;

    Updated above code to replace $! with $? (thanks AnomalousMonk).