in reply to Re^2: Loop stops at first iteration
in thread Loop stops at first iteration
In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
for (1..4) { if (glob($0)) { print("I exist\n"); } else { print("I don't exist\n"); } }
outputs
I exist I don't exist I exist I don't exist
if (glob(...)) is wrong. The only time glob should be in scalar context is in while (glob(...)), and the loop better not execute last, return or die.
Fix:
for (1..4) { if (() = glob($0)) { print("I exist\n"); } else { print("I don't exist\n"); } }
or
for (1..4) { if (-e $0) { print("I exist\n"); } else { print("I don't exist\n"); } }
I exist I exist I exist I exist
|
|---|