in reply to system bash return value

Why do you shell out when you can do it in pure Perl?

Just an example to be adapted to your specific needs:

my $word = "banjo"; my $found_it = pseudo_bash_grep ($word, "words.txt"); print "Found $word in the words.txt file \n" if $found_it; sub pseudo_bash_grep { my ($keyword, $file) = @_; open my $FILE_IN, "<", $file or die "Failed to open $file $!"; while (<$FILE_IN>) { return 1 if /$keyword/; } return 0; }
Result:
$ perl search.pl Found banjo in the words.txt file

Replies are listed 'Best First'.
Re^2: system bash return value
by haukex (Archbishop) on Dec 20, 2016 at 12:09 UTC

    Hi Laurent_R,

    I second this answer. Just a reminder to Mind the meta!, otherwise the function is the rough equivalent of grep -P, which may or may not be what the OP wants.

    Regards,
    -- Hauke D

      Hi,

      I'm not quite sure why you refer to this post (Mind the meta!).

      The code I suggested in my post was, as I pointed out, "(j)ust an example to be adapted to (the OP's) specific needs". Depending on the situation, the regex might need an ignore case modifier, a quotemeta of the string used for the regex, etc. And the code might need some other changes, too.

        Hi Laurent_R,

        Depending on the situation, the regex might need ... a quotemeta of the string used for the regex

        That's all I was trying to say, and I refer to Mind the meta! because in my experience many wisdom seekers are not aware of that issue, and I've seen monks forget \Q...\E several times too. As I said, your post is what I would have replied to the OP, so ++!

        Regards,
        -- Hauke D

Re^2: system bash return value
by shawnhcorey (Friar) on Dec 21, 2016 at 13:57 UTC
      Yeah, right, as I said already yesterday (Dec 20, 2016 at 14:49 UTC):
      Depending on the situation, the regex might need an ignore case modifier, a quotemeta of the string used for the regex, etc.