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

Hi all - Newbie here. I have written 4 perl scripts and am combining them in a main executed perl script. Do you think this is ok?
#!/usr/local/bin/perl do '1-search_user.pl'; do '2-prepare_2update_user.pl'; do '3-backup.pl'; do '4-run_update.pl';
The above works but
Q1. how do I make sure when there is an error in any of the scripts it will exit the entire program?
Q2. Does the above really executes it sequentially? To me I think so.
Q3. I plan to put it in a cron job to only execute the above called "0-main_prog.pl", do you think there would be any issues?

Please advice

Replies are listed 'Best First'.
Re: Executing multiple scripts sequentially
by hipowls (Curate) on Jan 18, 2008 at 09:47 UTC

    The short answer is make each script exit with 1 for success or 0 on error and then

    do '1-search_user.pl' or die; do '2-prepare_2update_user.pl' or die; do '3-backup.pl' or die; do '4-run_update.pl' or die;
    But that isn't really the best solution. I would either combine the four scripts into one script or put the functionality into a module. The first solution looks like
    search_user(); prepare_2update_user(); backup(); run_update(); sub search_user { ... } etc
    Are these scripts used elsewhere? If so then I'd go for a module and the script would then look like.
    use MyModule; MyModule::search_user(); MyModule::prepare_2update_user(); MyModule::backup(); MyModule::run_update();
    In either case each function throws it's own exception so you don't check for errors if all you want to do is exit.

    To get started with modules see Simple Module Tutorial

    The script executes sequentially and it can be run from cron, you will need to ensure that it can find the other scripts, environment variables may not be set so do '/path/to/script' or push @INC, '/path/to' may be necessary.

Re: Executing multiple scripts sequentially
by alexm (Chaplain) on Jan 18, 2008 at 13:19 UTC
    Provided that each .pl file has permission to be executed, you can easily achieve your purpose by using something like this:
    #!/bin/sh # stop on first non-zero exit status set -e 1-search_user.pl 2-prepare_2update_user.pl 3-backup.pl 4-run_update.pl
    Update: This could be a Perl version if each .pl returns a true value:
    #!/usr/bin/perl do '1-search_user.pl' and do '2-prepare_2update_user.pl' and do '3-backup.pl' and do '4-run_update.pl' or die "error occurred\n";
    I found the sh version much simpler.
    A reply falls below the community's threshold of quality. You may see it by logging in.