I recently came across an interesting line of code that waits until a particular file springs to life:
sleep 1 until -e $file;
While at face value the line does what one expects it to, I can’t but help feel that something is not right here; it just seems more natural to write the following:
while (1) {
sleep 1;
last if -e $file;
}
Are the file test operators intended for use only with if and unless? And are there any performance penalties in deploying such operators within while conditionals?
There is no difference between using a file test operator in a while-true (
while) vs a while-false (until) loop, or by separating out the test with anif/unless, nor is there a difference with anything else in Perl.The only magical case with the while loop is
... while <$handle>;orwhile (<$handle>) {...}which sets$_to the next line while it is defined. And it doesn’t really make sense to use the while-false (until) loop with bare handles.The
a until bconstruct is simplya while not b.To convert
sleep 1 until -e $file;into awhileloop, you would use the following:This can then be expanded to the block form:
Or as an infinite
whilewith internal escape:And all of these forms are equivalent.
Answering the rest of your question, most file systems should be fine with 1 second sleeps between checks, but you many way to pick a larger number if the process is not urgently waiting on the file. Also if you are on a device with a slow file system (stored in flash memory or via slow network) you might want to experiment with larger values as well.
And as
ysthnitpicks below, in general, statement forms return differently than block forms when they are the last element of a construct that returns:do {...},sub {...},do FILE, eval …