Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.8 KiB

  1. package IPC::Open2;
  2. use strict;
  3. use vars qw($VERSION @ISA @EXPORT);
  4. require 5.000;
  5. require Exporter;
  6. $VERSION = 1.01;
  7. @ISA = qw(Exporter);
  8. @EXPORT = qw(open2);
  9. =head1 NAME
  10. IPC::Open2, open2 - open a process for both reading and writing
  11. =head1 SYNOPSIS
  12. use IPC::Open2;
  13. $pid = open2(\*RDR, \*WTR, 'some cmd and args');
  14. # or
  15. $pid = open2(\*RDR, \*WTR, 'some', 'cmd', 'and', 'args');
  16. =head1 DESCRIPTION
  17. The open2() function spawns the given $cmd and connects $rdr for
  18. reading and $wtr for writing. It's what you think should work
  19. when you try
  20. open(HANDLE, "|cmd args|");
  21. The write filehandle will have autoflush turned on.
  22. If $rdr is a string (that is, a bareword filehandle rather than a glob
  23. or a reference) and it begins with ">&", then the child will send output
  24. directly to that file handle. If $wtr is a string that begins with
  25. "<&", then WTR will be closed in the parent, and the child will read
  26. from it directly. In both cases, there will be a dup(2) instead of a
  27. pipe(2) made.
  28. open2() returns the process ID of the child process. It doesn't return on
  29. failure: it just raises an exception matching C</^open2:/>.
  30. =head1 WARNING
  31. It will not create these file handles for you. You have to do this yourself.
  32. So don't pass it empty variables expecting them to get filled in for you.
  33. Additionally, this is very dangerous as you may block forever.
  34. It assumes it's going to talk to something like B<bc>, both writing to
  35. it and reading from it. This is presumably safe because you "know"
  36. that commands like B<bc> will read a line at a time and output a line at
  37. a time. Programs like B<sort> that read their entire input stream first,
  38. however, are quite apt to cause deadlock.
  39. The big problem with this approach is that if you don't have control
  40. over source code being run in the child process, you can't control
  41. what it does with pipe buffering. Thus you can't just open a pipe to
  42. C<cat -v> and continually read and write a line from it.
  43. =head1 SEE ALSO
  44. See L<IPC::Open3> for an alternative that handles STDERR as well. This
  45. function is really just a wrapper around open3().
  46. =cut
  47. # &open2: tom christiansen, <[email protected]>
  48. #
  49. # usage: $pid = open2('rdr', 'wtr', 'some cmd and args');
  50. # or $pid = open2('rdr', 'wtr', 'some', 'cmd', 'and', 'args');
  51. #
  52. # spawn the given $cmd and connect $rdr for
  53. # reading and $wtr for writing. return pid
  54. # of child, or 0 on failure.
  55. #
  56. # WARNING: this is dangerous, as you may block forever
  57. # unless you are very careful.
  58. #
  59. # $wtr is left unbuffered.
  60. #
  61. # abort program if
  62. # rdr or wtr are null
  63. # a system call fails
  64. require IPC::Open3;
  65. sub open2 {
  66. my ($read, $write, @cmd) = @_;
  67. local $Carp::CarpLevel = $Carp::CarpLevel + 1;
  68. return IPC::Open3::_open3('open2', scalar caller,
  69. $write, $read, '>&STDERR', @cmd);
  70. }
  71. 1