That first test puts glob in a scalar context. Each time it is called, it'll return the next item of the directory, until it returns undef. Thus, you'll get inconsistent results.
Don't use glob in a scalar context unless you plan to drain it each time in a loop.