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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.