That's not really a wait issue. When closing a piped open filehandle, if the opened command exits with a non-zero and that's the only issue with the command, then $! is set to 0. So the code above is using the tertiary operater to check that condition. I thought that might be the issue too but the OP said it worked fine from the command line so more than likely there's either a path or permisssion issue.
The code jesuashok posted was simply copied verbatim from the example section of perldoc entry for close, even to the point of including the comment containing the ellipses "#...":
open(OUTPUT, '|sort >foo') # pipe to sort
or die "Can't start sort: $!";
#... # print stuff to output
close OUTPUT # wait for sort to finish
or warn $! ? "Error closing sort pipe: $!"
: "Exit status $? from sort";
open(INPUT, 'foo') # get sort's results
or die "Can't open 'foo' for input: $!";