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

Hi all, My doubt is, How can I write a Perl script in which I can call couple of batch files? Lets say I have 2 .bat files "Import.bat" and "Export.bat"......The former imports the data from one server and latter exports the data to the other...... I need to write a Perl script in which I want both this files to run in a sequence as mentioned. How do I achieve this? Now another thing........Instead of calling these batch files, if I write my own Perl script that does the same task of these .bat files, will this be more efficient? Please assist which is the better way to do it on windows. Thanks Ethen

Replies are listed 'Best First'.
Re: Callin batch files from Perl script
by davido (Cardinal) on Dec 19, 2007 at 07:50 UTC

    See the system command, or perlop's discussion of backticks. Either will allow you to execute a batch file.

    As for whether it's better to compose a batch file, or handle all the work directly within perl, I would probably opt for the latter most of the time. However, without knowing more about your problem it's impossible to say definitively which approach is better. I generally find it less complex to just do the work in one scripting language instead of two. But that doesn't mean there are no cases where invoking a batch file is a good way to do it.

    In particular, if the objective is just to get the job done, and you already happen to have a couple batch files written that are doing the job for you, there's nothing wrong with simply using perl as the glue to bind them together. If function is more important than fashion, use the tools you already have at your fingertips, and glue them together with Perl.


    Dave

Re: Callin batch files from Perl script
by KurtSchwind (Chaplain) on Dec 19, 2007 at 14:03 UTC

    David's right. You are looking for the system command.

    As to whether tis nobler in the hearts and minds to use Perl pure instead of systeming out to some batch files: Consider this. Your batch files are probably calling executables which, if re-written in perl, your perl program would have to system out to do anyway. Also, if your batch files are working just fine, there is no shame in using perl for process control (which is what we call it if perl is directing the other processes).

    I'd probalby convert the batch apps to perl based on how much 'thinking' is done in the batch file. If the batch file is just

    date /t > c:\temp\foo_export.log time /t >> c:\temp\foo_export.log export foo_database > c:\temp\foo_database_export >> c:\temp\foo_expor +t.log date /t >> c:\temp\foo_export.log time /t >> c:\temp\foo_export.log
    Or something simliar, I'd probably just leave it as a batch. But if it's checking for the existence of one file and creating an argument list on the fly based on some flags or something, I might opt to switch to perl. Put bluntly, perl is a LOT smarter than a dos batch file can be.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.