Exiting a perl script during system commands

I have a script that calls system() in a loop, and the system call I'm using takes 3+ seconds to run. If I want to abort the script and I hit CTRL + C it stops executing the system() command and goes back to perl, which then loops and starts system() again. A great solution to this problem is to die() in your perl if script exits abnormally.

for ($i = 0; $i < 100; $i++) {
    $exit = system('sleep 3');
    if ($exit != 0) { die("\nsystem() call died, so we're dying too\n"); }
}

Props to Bleything.net for finding such an elegant solution.

Leave A Reply
All content licensed under the Creative Commons License