Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How can I redirect STDOUT and STDERR from a program on WIN32?

by Anonymous Monk
on Jan 24, 2001 at 07:26 UTC ( [id://53870]=note: print w/replies, xml ) Need Help??


in reply to How can I redirect STDOUT and STDERR from a program on WIN32?

I wrote this for unix:
sub phork {
  # we will call fork and return a pid.  The child will exec with all args 
  # and suppress the child's output (with /dev/null);
  my $pid;
  
  if ($pid = fork) {                    # fork the process;
    #parent
    return $pid;
  }else    
  {
    #child
    die "CANNOT FORK!!\n" unless defined $pid;
    open(STDOUT, "/dev/null");          # suppressing output  
    open(STDERR, "/dev/null");          # suppressing output
    {exec(@_);};                        # calls exec with current @_
    exit(1);                            # exec may maybe fail... maybe.      
  }  
}   
now, if you used the opens to open pipes, or to open files you want the output in, I'm thinking it should work in windows too.

I have a question, however, regarding how one would just disregard STDOUT and STDERR under windows using this function (there's no /dev/null of course).

Also, when you reap the forked process, you can then get the return value ($exit_value = $? >> 8;) if you don't want asynchronocity, then replace exec with system, and you can get the return value directly from that.

-Daniel

  • Comment on Re: How can I redirect STDOUT and STDERR from a program on WIN32?

Replies are listed 'Best First'.
Re: Answer: How can I redirect STDOUT and STDERR from a program on WIN32?
by repson (Chaplain) on Jan 24, 2001 at 08:37 UTC
    On windows the null device is simply 'NUL' I belive.

    To make it portable do this instead.

    use File::Spec; sub phork { #... open(STDOUT, '> ' . File::Spec->devnull); open(STDERR, '> ' . File::Spec->devnull); #... }
    That _should_ do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://53870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found