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

Hi,I am using a perforce command "p4 where" as part of my script.I see the output getting stored in the variable "p4where_output" when the command is successful in the below code but when the command fails I see the following getting printed on the screen,can someone advise how to stop printing and capture the output even when the command fails?

OUTPUT on screen when the command fails:- //files/data.c - file(s) not in client view.
my $p4where_output=`p4 where $file`; if($p4where_output =~ /file(s) not in client view/) { push @changed_paths,"$file\n"; }

Replies are listed 'Best First'.
Re: How to stop printing the output of a command on screen when the command fails?
by roboticus (Chancellor) on Dec 24, 2010 at 05:06 UTC

    perl_mystery:

    Redirect the STDERR stream. In bash, for example, you can do:

    p4 where filename 2>&1

    This tells the p4 command to send the STDERR stream (handle 2) to handle 1 (STDOUT). I expect that the same syntax will likely work in backticks, but I've never tried it, so you'll need to verify that. There are multiple ways to redirect the I/O streams using perl, so you may want to read perldoc perlipc and perldoc -f open to start with.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Roboticus,your suggesting doesnt seem to working,why is it so?

      foreach my $file (@changed_files) { my $p4where_output=`p4 where $file 2>&1`; print "P4 where output:$p4where_output\n"; if($p4where_output =~ /file(s) not in client view/) { push @changed_paths,"$file\n"; }
        Roboticus's suggestion should have worked, and is the simplest solution. Some programs (not many) do not write to stderr, but to the terminal driver direct. Check the documentation for the product you are using that it is actually writing to stderr. If that does not tell you, then run it under strace(1) or truss(1) to check (these are UNIX/Linux commands which trace kernel calls).
Re: How to stop printing the output of a command on screen when the command fails?
by GrandFather (Saint) on Dec 24, 2010 at 06:18 UTC

    $p4where_output =~ /file(s) not in client view/ probably is not doing what you think. The (s) matches and captures an 's' so the string matched is 'files not in client view' as I suspect you expect. More likely the regex you want is /files\(s\) not in client view/ to match the brackets. In fact it looks like you simply need a string match so you could: $p4where_output eq 'file(s) not in client view' instead.

    True laziness is hard work

      Thanks for the suggestion grandfather but my problem is even after using 2>&1 as shown above,I still see stderr (not in client view messages)getting printed on the command prompt.I want to save it to the same variable "$p4where_output" and then proceed further

        I wasn't addressing your primary problem (although if you are using linux then try &2>1), but what appears to be a separate bug that will manifest itself when the current blocker bug is fixed.

        True laziness is hard work
Re: How to stop printing the output of a command on screen when the command fails?
by Anonymous Monk on Dec 24, 2010 at 05:02 UTC
    redirect stderr to null
    my $null = File::Spec->devnull; my $out = ` ... 2>$null`;

      I want to save the stderr output.I want to store the output and match it to "file(s) not in client view" ,based on this match i want to proceed further