stanley82 has asked for the wisdom of the Perl Monks concerning the following question:

I need to find if a keyword exists in a file.

With bash, grep keykword file.txt returns 0 to bash on success and 1 or 2 on fail.

In cpp int i = system("grep something file.txt"); //gets the return value in i.

With perl I'm sure @i will capture what normally goes to the terminal stdout. I only need to know if "something" is in "file.txt" or not, any ideas reverend father?

Your son Ian.

Replies are listed 'Best First'.
Re: system bash return value
by Laurent_R (Canon) on Dec 20, 2016 at 09:08 UTC
    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

      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.

        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.
Re: system bash return value
by LanX (Saint) on Dec 20, 2016 at 02:45 UTC
    Use either system or qx (aka backticks) and check $?

    From perlop#Quote-Like-Operators

    > Like system, backticks put the child process exit code in $? . If you'd like to manually inspect failure, you can check all possible failure modes by inspecting $? like this:

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: system bash return value
by duyet (Friar) on Dec 20, 2016 at 06:46 UTC
    found result:
    my $rc = system("grep foobar debug.log"); print "rc = '$rc'\n"; 2016/10/31 13:47:21 -5168 [19] Tools.pm 22 Tools.run_test - running te +st baz with parameters foobar rc = '0'
    not found result:
    my $rc = system("grep blabla debug.log"); print "rc = '$rc'\n"; rc = '256'
Re: system bash return value
by Anonymous Monk on Dec 20, 2016 at 02:35 UTC
    Capture::Tiny
    #!/usr/bin/perl -- use strict; use warnings; use Capture::Tiny qw/ capture /; ... my @cmd = ( '/usr/bin/echo', 'whiskey', 'foxtrot' ); my( $stdout, $stderr, $exit ) = capture { system { $cmd[0] } @cmd; };; ...