According to perlop, this is exactly what it does - it will keep going until the end before starting over. Thus, you want something like:
$rc = 1;
while (defined($nextfile = <$dirName/*cm*.4gl>))
{
if ($rc) {
$rc = system("grep $field $nextfile 1> /dev/null");
if (!$rc) {
print FOUND "$field $nextfile\n";
}
}
}
But I still don't like it. Use
glob rather than the
<> operator.
for my $nextfile (glob File::Spec->catfile($dirName, '*cm*.4gl')) {
$rc = system("grep $field $nextfile 1> /dev/null");
if (!$rc) {
print FOUND "$field $nextfile\n";
last;
}
}
I'd also recommend a few minor other changes:
- Opening for reading: make it explicit with "<". open(FIELDS,"<fields.dat");. Also, use IO::File. But I just like the objects better.
- Your while can assign directly to $field: while (my $field = <FIELDS>). Don't use $_ if you're not using $_.
- Use the array version of system. To do this, you'll need a full path to grep, and you'll need to redirect STDOUT yourself. Arguably not worth the effort (it is a lot of effort if you want to use STDOUT elsewhere). But all it takes is getting bit merely once by a shell interpolation of something you didn't expect to make the benefits all too obvious.
Hope this helps.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.