Perl: After a successful system call, "or die" command still ends script

user3481208 picture user3481208 · Mar 31, 2014 · Viewed 14.8k times · Source

I am using the following line to make a simple system call which works:

system ("mkdir -p Purged") or die "Failed to mkdir." ;

Executing the script does make the system call and I can find a directory called Purged, but the error message is still printed and the script dies. What is wrong with my syntax?

Answer

choroba picture choroba · Mar 31, 2014

system returns the exit status of the command it calls. In shell, zero exit status means success. You have to invert the logic:

0 == system qw(mkdir -p Purged) or die "Failed to create the dir\n";