home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _ef496d123e1fc8e68ae09bbd5dd159b5 < prev    next >
Text File  |  2000-03-23  |  5KB  |  116 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>open2 - open a process for both reading and writing</TITLE>
  5. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> open2 - open a process for both reading and writing</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <LI><A HREF="#warning">WARNING</A></LI>
  26.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  27. </UL>
  28. <!-- INDEX END -->
  29.  
  30. <HR>
  31. <P>
  32. <H1><A NAME="name">NAME</A></H1>
  33. <P>IPC::Open2, open2 - open a process for both reading and writing</P>
  34. <P>
  35. <HR>
  36. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  37. <UL>
  38. <LI>Linux</LI>
  39. <LI>Solaris</LI>
  40. <LI>Windows</LI>
  41. </UL>
  42. <HR>
  43. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  44. <PRE>
  45.     use IPC::Open2;</PRE>
  46. <PRE>
  47.     $pid = open2(\*RDRFH, \*WTRFH, 'some cmd and args');
  48.       # or without using the shell
  49.     $pid = open2(\*RDRFH, \*WTRFH, 'some', 'cmd', 'and', 'args');</PRE>
  50. <PRE>
  51.     # or with handle autovivification
  52.     my($rdrfh, $wtrfh);
  53.     $pid = open2($rdrfh, $wtrfh, 'some cmd and args');
  54.       # or without using the shell
  55.     $pid = open2($rdrfh, $wtrfh, 'some', 'cmd', 'and', 'args');</PRE>
  56. <P>
  57. <HR>
  58. <H1><A NAME="description">DESCRIPTION</A></H1>
  59. <P>The <CODE>open2()</CODE> function runs the given $cmd and connects $rdrfh for
  60. reading and $wtrfh for writing.  It's what you think should work 
  61. when you try</P>
  62. <PRE>
  63.     $pid = open(HANDLE, "|cmd args|");</PRE>
  64. <P>The write filehandle will have autoflush turned on.</P>
  65. <P>If $rdrfh is a string (that is, a bareword filehandle rather than a glob
  66. or a reference) and it begins with <CODE>>&</CODE>, then the child will send output
  67. directly to that file handle.  If $wtrfh is a string that begins with
  68. <CODE><&</CODE>, then $wtrfh will be closed in the parent, and the child will read
  69. from it directly.  In both cases, there will be a <CODE>dup(2)</CODE> instead of a
  70. <A HREF="../../lib/Pod/perlfunc.html#item_pipe"><CODE>pipe(2)</CODE></A> made.</P>
  71. <P>If either reader or writer is the null string, this will be replaced
  72. by an autogenerated filehandle.  If so, you must pass a valid lvalue
  73. in the parameter slot so it can be overwritten in the caller, or
  74. an exception will be raised.</P>
  75. <P><CODE>open2()</CODE> returns the process ID of the child process.  It doesn't return on
  76. failure: it just raises an exception matching <CODE>/^open2:/</CODE>.  However,
  77. <A HREF="../../lib/Pod/perlfunc.html#item_exec"><CODE>exec</CODE></A> failures in the child are not detected.  You'll have to
  78. trap SIGPIPE yourself.</P>
  79. <P><CODE>open2()</CODE> does not wait for and reap the child process after it exits.
  80. Except for short programs where it's acceptable to let the operating system
  81. take care of this, you need to do this yourself.  This is normally as
  82. simple as calling <CODE>waitpid $pid, 0</CODE> when you're done with the process.
  83. Failing to do this can result in an accumulation of defunct or ``zombie''
  84. processes.  See <A HREF="../../lib/Pod/perlfunc.html#waitpid">waitpid in the perlfunc manpage</A> for more information.</P>
  85. <P>This whole affair is quite dangerous, as you may block forever.  It
  86. assumes it's going to talk to something like <STRONG>bc</STRONG>, both writing
  87. to it and reading from it.  This is presumably safe because you
  88. ``know'' that commands like <STRONG>bc</STRONG> will read a line at a time and
  89. output a line at a time.  Programs like <STRONG>sort</STRONG> that read their
  90. entire input stream first, however, are quite apt to cause deadlock.</P>
  91. <P>The big problem with this approach is that if you don't have control 
  92. over source code being run in the child process, you can't control
  93. what it does with pipe buffering.  Thus you can't just open a pipe to
  94. <CODE>cat -v</CODE> and continually read and write a line from it.</P>
  95. <P>The IO::Pty and Expect modules from CPAN can help with this, as they
  96. provide a real tty (well, a pseudo-tty, actually), which gets you
  97. back to line buffering in the invoked command again.</P>
  98. <P>
  99. <HR>
  100. <H1><A NAME="warning">WARNING</A></H1>
  101. <P>The order of arguments differs from that of open3().</P>
  102. <P>
  103. <HR>
  104. <H1><A NAME="see also">SEE ALSO</A></H1>
  105. <P>See <A HREF="../../lib/IPC/Open3.html">the IPC::Open3 manpage</A> for an alternative that handles STDERR as well.  This
  106. function is really just a wrapper around open3().</P>
  107. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  108. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  109. <STRONG><P CLASS=block> open2 - open a process for both reading and writing</P></STRONG>
  110. </TD></TR>
  111. </TABLE>
  112.  
  113. </BODY>
  114.  
  115. </HTML>
  116.