U
usenet
If I try to iterate a loop over an empty list, Perl just skips the
loop. Is there any way to know that's what happened (without kludges
such as setting counters or bool flags within the loop)?
What I would like to do is something 'elegant' like:
for (glob '*.txt') {
... do some stuff with the files ...
}else{ #Iwish I could do this!
warn "No files found!\n";
}
But, of course, I can't do use an else in a for loop. I can think of
all sorts of UGLY ways to accomplish this (like putting a counter in
the loop or assigning the glob to an array and then warning if the
array is empty), but I prefer something elegant that doesn't require
the creation of a special variable just to know if the loop iterated.
Anyone have any suggestions?
loop. Is there any way to know that's what happened (without kludges
such as setting counters or bool flags within the loop)?
What I would like to do is something 'elegant' like:
for (glob '*.txt') {
... do some stuff with the files ...
}else{ #Iwish I could do this!
warn "No files found!\n";
}
But, of course, I can't do use an else in a for loop. I can think of
all sorts of UGLY ways to accomplish this (like putting a counter in
the loop or assigning the glob to an array and then warning if the
array is empty), but I prefer something elegant that doesn't require
the creation of a special variable just to know if the loop iterated.
Anyone have any suggestions?