Since $x contains the results of the first gzgrep, you can rely on its result instead of gzgrep-ing twice, as in:
$x = `gzgrep $y <PATH>`; if( not $x ) { $x= `gzgrep -i <TERM> <PATH>`; }
Or use a logical short-circuit '||' operator like this:
$x = `gzgrep $y <PATH>` || `gzgrep -i <TERM> <PATH>`;
This would work because when it evaluates a logical OR operator, Perl stop as soon as it finds success. In other words, $x will be assigned the value of the first gzgrep if it is successful, or if it's unsuccessful, the second gzgrep will be executed, and $x will be assigned its value.
By the way: Stop everything you're doing, put the following at the top of your script:
use strict; use warnings;
And then convert your variables to lexicals, as in, "my $x;" You'll thank yourself later on. Hopefully you're not so far into something that it would create a huge amount of work for you. Doing this will require that you pass values into functions through parameters instead of global osmosis, and return values through the 'return' or through parameter modification (where necessary). It may break what you're already working on to the point that you need to rewrite it. But if what you're working on is potentially going to grow into a larger project, it's worth refactoring to use lexicals instead of globals where possible.
Dave
In reply to Re: Perl/Unix question; returning grep as a boolean and returning a match
by davido
in thread Perl/Unix question; returning grep as a boolean and returning a match
by limzz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |