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

Hi all
I have simple find function, seeking out clearcase `view private` files in a directory tree.
I am using backticks to query the files.
My only problem is: the backticks are echoing the stderr from the command, which I don't want.
How can I make it stop ?
Here is the code:

find({wanted => \&viewscan,follow}, $view); sub viewscan { my $fname = $File::Find::name; my $cmd = `cleartool ls -l $fname`; my @fileinfo = grep {$_ =~ /view private/} $cmd; }

Replies are listed 'Best First'.
Re: backticks are echoing stderr
by Anonymous Monk on Aug 26, 2008 at 08:04 UTC
    redirect stderr to a file, or devnull (File::Spec->devnull();) something like
    require File::Spec; my $devnull = File::Spec->devnull(); my $cmd = `cleartool ls -l $fname 2>$devnull`;
      Thanks !!
      It works.
      This is the first time I posted a question here, and already it was worth it !
      Keep up the good work :)
Re: backticks are echoing stderr
by Bloodnok (Vicar) on Aug 26, 2008 at 09:11 UTC
    Hi ,

    For a slightly more complicated, but platform agnostic (I do like that expression - thanx again bart) solution, why not try...

    find({wanted => \&viewscan,follow}, $view); sub viewscan { open CMD, "cleartool ls -l $fname 2>&1 |", or die(...); my @fileinfo = <CMD>; close CMD; @fileinfo = grep /view private/, @fileinfo; }
    or maybe, at a push ...
    find({wanted => \&viewscan,follow}, $view); sub viewscan { open CMD, "cleartool ls -l $fname 2>&1 |", or die(...); my @fileinfo = grep /view private/, (<CMD>); close CMD; }

    HTH ,

    A user level that continues to overstate my experience :-))
Re: backticks are echoing stderr
by Tanktalus (Canon) on Aug 26, 2008 at 15:39 UTC

    You can find more information on the backticks from "perldoc perlop" (look for "qx"). Quote: "The collected standard output of the command is returned; standard error is unaffected." It then goes on to give a bunch of examples, including the answer to your question.

    I think there are a few reasons for this behaviour. First off, you generally don't want to hide the stderr. If there is an error in the command you're running, you probably DO want it to leak through to the person running the script - it may have very useful information as to why your code is acting funny. Second, it serves as a reminder to us, the perl developers, that there's really nothing we can do to block all output. If an application really wants to, it can open a brand new filehandle to the current (pseudo-)terminal device, which seems really easy on Linux - get your parent's pid, and open /proc/$ppid/fd/1 for writing:

    $ perl -le 'open my $fh, ">", "/proc/" . getppid . "/fd/1" or die $!;p +rint $fh "<Nelson>Ha ha!</Nelson>"' > /dev/null 2> /dev/null <Nelson>Ha ha!</Nelson>
    It may take a bit more work on other platforms, but I'm sure that most unixy platforms will allow you to find the current terminal and write to it (barring a change of session ID or UID). Of course, this is fairly pathological, and I doubt Rational would do this, but it's still something to note.

Re: backticks are echoing stderr
by didess (Sexton) on Aug 26, 2008 at 08:57 UTC
    Hi, simpler, although working identically on UNIX-likes (incl. LINUX)
    my $returned = `YOUR_COMMAND_HERE 2>/dev/null`;
Re: backticks are echoing stderr
by Mr. Muskrat (Canon) on Aug 26, 2008 at 17:42 UTC

    It looks like you are trying to write your own version of the lsprivate cleartool command.

    cleartool lsprivate [/vobs/whatever/path/you/like]

    When you run it with no path, it will show all private files in your view. When you run it with a path, it will only show you the private files in your view in that portion of the directory tree.

      It looks like you are trying to write your own version of the lsprivate cleartool command.

      Clippy, is that you?

      It can't be Clippy, the response was helpful and insightful. It must really be Mr. Muskrat.


      TGI says moo