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

Fellow Monks,

I am working on some old scripts, they're using a master perl script to call a load of other perl scripts, using system calls. For example:
print"script1.pl\n"; system("script1.pl"); print"script2.pl\n"; system("script2.pl"); print"script3.pl\n"; system("script3.pl"); etc...
In total about 20 scripts are called. Is it good practice to use system calls this way? What other methods are there for calling a load of scripts?

Thanks in advance,
Plato.

Replies are listed 'Best First'.
Re: system calls question
by gellyfish (Monsignor) on Oct 08, 2004 at 09:04 UTC

    If you are purely running perl programs with no arguments then you might want to consider using do [filename] which won't fork a new perl interpreter.

    /J\

Re: system calls question
by Mutant (Priest) on Oct 08, 2004 at 11:15 UTC
    A lot of it depends on what you're doing with the scripts. In most cases, forking isn't really necessary, so as someone suggested:

    do 'script1.pl'

    If the master script is some sort of daemon (ie. it doesn't exit after being called), then forking is probably a good idea, since the memory will be freed after the script has executed.

    Unless these are something other than very basic system admin type scripts, you may want to consider re-coding the scripts as modules, and call them as a function.
Re: system calls question
by Grygonos (Chaplain) on Oct 08, 2004 at 12:57 UTC

    I'm really going to agree that it's just as efficient to run these via cron/batch/shell. If there is a specific purpose to running them from a perl script I would suggest the do 'x.pl' or forking (depending on your needs) methods described above.

Re: system calls question
by Jasper (Chaplain) on Oct 08, 2004 at 09:17 UTC
    cron?

    edit: wow, losing XP hand over fist with that.. But seriously, what's wrong with shell, or a batch file, or whatever? Isn't this exactly the sort of thing they'll do better than perl?
Re: system calls question
by revdiablo (Prior) on Oct 08, 2004 at 17:51 UTC
    What other methods are there for calling a load of scripts?

    You could convert the scripts into modules that follow some specific API, then load each module and start it doing its work. For example, you could have a run function in each one that starts it off.

    Then voila, you have a basic plugin system. Make a certain directory to put the plugins, have your main script glob the directory and run each one. Next time you need to create a new "script", it's as easy as creating a module and saving it to the right place.

Re: system calls question
by hostyle (Scribe) on Oct 09, 2004 at 12:57 UTC
    foreach ("script1.pl", "script2.pl", "script3.pl") { print "$_ \n"; require; }