open and STDERR

Discussion in 'Perl Misc' started by grocery_stocker, Nov 12, 2006.

  1. "Found in /usr/lib/perl5/5.8.3/pod/perlfaq8.pod
    How can I capture STDERR from an external command?

    There are three basic ways of running external commands:

    system $cmd; # using system()
    $output = ‘$cmd‘; # using backticks (‘‘)
    open (PIPE, "cmd │"); # using open()

    With system(), both STDOUT and STDERR will go the same
    place as the script’s STDOUT and STDERR, unless the sys­
    tem() command redirects them. Backticks and open() read
    only the STDOUT of your command."

    If open() reads only to STDOUT, Then how when I do something like the
    following:

    #!/usr/bin/perl -w

    open(STDERR, "whore.txt") or die "Cant: $! \n";

    I get:
    miss_xtc@linux:~/perl> ./op.pl
    Cant: No such file or directory

    Wouldn't this be writing to STDERR in this case?
     
    grocery_stocker, Nov 12, 2006
    #1
    1. Advertising

  2. grocery_stocker

    -berlin.de Guest

    grocery_stocker <> wrote in comp.lang.perl.misc:
    > "Found in /usr/lib/perl5/5.8.3/pod/perlfaq8.pod
    > How can I capture STDERR from an external command?
    >
    > There are three basic ways of running external commands:
    >
    > system $cmd; # using system()
    > $output = ‘$cmd‘; # using backticks (‘‘)
    > open (PIPE, "cmd │"); # using open()
    >
    > With system(), both STDOUT and STDERR will go the same
    > place as the script’s STDOUT and STDERR, unless the sys­
    > tem() command redirects them. Backticks and open() read
    > only the STDOUT of your command."
    >
    > If open() reads only to STDOUT, Then how when I do something like the
    > following:
    >
    > #!/usr/bin/perl -w
    >
    > open(STDERR, "whore.txt") or die "Cant: $! \n";


    That tries to open "whore.txt" for reading, which fails unless the
    file already exists.

    > I get:
    > miss_xtc@linux:~/perl> ./op.pl
    > Cant: No such file or directory
    >
    > Wouldn't this be writing to STDERR in this case?


    Why should it? You have to open the file for writing:

    open STDERR, '>', 'whore.txt' or die "Can't create 'whore.txt': $!";

    Anno
     
    -berlin.de, Nov 12, 2006
    #2
    1. Advertising

  3. -berlin.de wrote:
    > grocery_stocker <> wrote in comp.lang.perl.misc:
    > > "Found in /usr/lib/perl5/5.8.3/pod/perlfaq8.pod
    > > How can I capture STDERR from an external command?
    > >
    > > There are three basic ways of running external commands:
    > >
    > > system $cmd; # using system()
    > > $output = ‘$cmd‘; # using backticks (‘‘)
    > > open (PIPE, "cmd │"); # using open()
    > >
    > > With system(), both STDOUT and STDERR will go the same
    > > place as the script’s STDOUT and STDERR, unless the sys­
    > > tem() command redirects them. Backticks and open() read
    > > only the STDOUT of your command."
    > >
    > > If open() reads only to STDOUT, Then how when I do something like the
    > > following:
    > >
    > > #!/usr/bin/perl -w
    > >
    > > open(STDERR, "whore.txt") or die "Cant: $! \n";

    >
    > That tries to open "whore.txt" for reading, which fails unless the
    > file already exists.
    >
    > > I get:
    > > miss_xtc@linux:~/perl> ./op.pl
    > > Cant: No such file or directory
    > >
    > > Wouldn't this be writing to STDERR in this case?

    >
    > Why should it? You have to open the file for writing:
    >
    > open STDERR, '>', 'whore.txt' or die "Can't create 'whore.txt': $!";
    >
    > Anno


    If I understand the faq, something like
    $output = `$cmd`;

    Reads only the STDOUT. Where is STDERR going in this case?
     
    grocery_stocker, Nov 12, 2006
    #3
  4. grocery_stocker

    Eric Amick Guest

    On 12 Nov 2006 15:16:08 -0800, "grocery_stocker" <>
    wrote:

    >If I understand the faq, something like
    >$output = `$cmd`;
    >
    >Reads only the STDOUT. Where is STDERR going in this case?


    Nowhere, i.e., the bit bucket. If you want it to go somewhere, redirect
    stderr in the command according to the command shell's conventions. For
    example,

    $output = `$cmd 2>/some/file/or/other`

    on *nix sends stderr to /some/file/or/other. (This is all in perlop.)
    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, Nov 13, 2006
    #4
  5. grocery_stocker

    Joe Smith Guest

    grocery_stocker wrote:

    > If I understand the faq, something like
    > $output = `$cmd`;
    >
    > Reads only the STDOUT. Where is STDERR going in this case?


    STDERR from $cmd goes to the same place that your perl program's
    STDERR is going. Use

    $output = `$cmd 2>&1`;

    if you want STDERR to go to the same place as STDOUT. In that
    case, $output will have both, and your program may have to
    search for and/or remove the error messages from it.
    -Joe
     
    Joe Smith, Nov 13, 2006
    #5
  6. Joe Smith wrote:
    > grocery_stocker wrote:
    >
    > > If I understand the faq, something like
    > > $output = `$cmd`;
    > >
    > > Reads only the STDOUT. Where is STDERR going in this case?

    >
    > STDERR from $cmd goes to the same place that your perl program's
    > STDERR is going. Use
    >
    > $output = `$cmd 2>&1`;
    >
    > if you want STDERR to go to the same place as STDOUT. In that
    > case, $output will have both, and your program may have to
    > search for and/or remove the error messages from it.
    > -Joe


    Further down the perl faq, they give the following solution
    To capture a program's STDOUT, but discard its STDERR:

    use IPC::Open3;
    use File::Spec;
    use Symbol qw(gensym);
    open(NULL, ">", File::Spec->devnull);
    my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
    while( <PH> ) { }
    waitpid($pid, 0);

    they don't use
    $output = `$cmd 2>&1`;

    I'm assuming the solution is perl faq done for portability purposes. I
    don't have a Windows box,
    otherwise I would I would see if something like

    $output = `$cmd 2>&1`;

    would have worked in Windows XP
     
    grocery_stocker, Nov 13, 2006
    #6
  7. grocery_stocker

    Joe Smith Guest

    grocery_stocker wrote:
    > don't have a Windows box,
    > otherwise I would I would see if something like
    >
    > $output = `$cmd 2>&1`;
    >
    > would have worked in Windows XP


    It does.

    C:\>perl -le "$_=`net file 2>&1`;s/\s*$//;print'>>>',$_,'<<<'"
    >>>There are no entries in the list.<<<


    -Joe
     
    Joe Smith, Nov 13, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Newberry
    Replies:
    0
    Views:
    650
    Dave Newberry
    Oct 17, 2004
  2. Vincent Touquet
    Replies:
    1
    Views:
    591
    Adrian B.
    Sep 3, 2004
  3. Vincent  Touquet
    Replies:
    0
    Views:
    444
    Vincent Touquet
    Sep 6, 2004
  4. ZelluX
    Replies:
    15
    Views:
    821
    Arne Vajhøj
    Jun 25, 2008
  5. ids
    Replies:
    4
    Views:
    1,509
    Keith Thompson
    Apr 14, 2009
Loading...

Share This Page