home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / SubProcess.pm < prev    next >
Encoding:
Perl POD Document  |  2004-09-23  |  4.5 KB  |  180 lines

  1. # /*
  2. #  * *********** WARNING **************
  3. #  * This file generated by ModPerl::WrapXS/0.01
  4. #  * Any changes made here will be lost
  5. #  * ***********************************
  6. #  * 01: lib/ModPerl/Code.pm:701
  7. #  * 02: O:\147xampp\sources\mod_perl-1.99_16\blib\lib/ModPerl/WrapXS.pm:584
  8. #  * 03: O:\147xampp\sources\mod_perl-1.99_16\blib\lib/ModPerl/WrapXS.pm:1100
  9. #  * 04: Makefile.PL:335
  10. #  * 05: Makefile.PL:283
  11. #  * 06: Makefile.PL:51
  12. #  */
  13.  
  14.  
  15. package Apache::SubProcess;
  16.  
  17. use strict;
  18. use warnings FATAL => 'all';
  19.  
  20.  
  21.  
  22. use Apache::XSLoader ();
  23. our $VERSION = '0.01';
  24. Apache::XSLoader::load __PACKAGE__;
  25.  
  26. use APR::PerlIO ();
  27.  
  28.  
  29. 1;
  30. __END__
  31.  
  32. =head1 NAME
  33.  
  34. Apache::SubProcess -- Executing SubProcesses under mod_perl
  35.  
  36.  
  37.  
  38.  
  39.  
  40. =head1 Synopsis
  41.  
  42.   use Apache::SubProcess ();
  43.   
  44.   use Config;
  45.   use constant PERLIO_IS_ENABLED => $Config{useperlio};
  46.   
  47.   # pass @ARGV / read from the process
  48.   $command = "/tmp/argv.pl";
  49.   @argv = qw(foo bar);
  50.   $out_fh = $r->spawn_proc_prog($command, \@argv);
  51.   $output = read_data($out_fh);
  52.   
  53.   # pass environment / read from the process
  54.   $command = "/tmp/env.pl";
  55.   $r->subprocess_env->set(foo => "bar");
  56.   $out_fh = $r->spawn_proc_prog($command);
  57.   $output = read_data($out_fh);
  58.   
  59.   # write to/read from the process
  60.   $command = "/tmp/in_out_err.pl";
  61.   ($in_fh, $out_fh, $err_fh) = $r->spawn_proc_prog($command);
  62.   print $in_fh "hello\n";
  63.   $output = read_data($out_fh);
  64.   $error  = read_data($err_fh);
  65.   
  66.   # helper function to work w/ and w/o perlio-enabled Perl
  67.   sub read_data {
  68.       my($fh) = @_;
  69.       my $data;
  70.       if (PERLIO_IS_ENABLED || IO::Select->new($fh)->can_read(10)) {
  71.           $data = <$fh>;
  72.       }
  73.       return defined $data ? $data : '';
  74.   }
  75.  
  76.  
  77.  
  78.  
  79. =head1 Description
  80.  
  81. C<Apache::SubProcess> provides the Perl API for running and
  82. communicating with processes spawned from mod_perl handlers.
  83.  
  84. At the moment it's possible to spawn only external program in a new
  85. process. It's possible to provide other interfaces, e.g. executing a
  86. sub-routine reference (via C<B::Deparse>) and may be spawn a new
  87. program in a thread (since the APR api includes API for spawning
  88. threads, e.g. that's how it's running mod_cgi on win32).
  89.  
  90.  
  91. =head1 API
  92.  
  93.  
  94. =head2 C<spawn_proc_prog>
  95.  
  96. Spawn a sub-process and return STD communication pipes:
  97.  
  98.   $out_fh                    = $r->spawn_proc_prog($command);
  99.   $out_fh                    = $r->spawn_proc_prog($command, \@argv);
  100.   ($in_fh, $out_fh, $err_fh) = $r->spawn_proc_prog($command);
  101.   ($in_fh, $out_fh, $err_fh) = $r->spawn_proc_prog($command, \@argv);
  102.  
  103. =over 4
  104.  
  105. =item obj: C<$r>
  106. ( C<L<Apache::RequestRec object|docs::2.0::api::Apache::RequestRec>> )
  107.  
  108. =item arg1: C<$command> ( string )
  109.  
  110. The command to be C<$exec()>'ed.
  111.  
  112. =item opt arg2: C<\@argv> ( ARRAY ref )
  113.  
  114. A reference to an array of arguments to be passed to the process as
  115. the process' C<ARGV>.
  116.  
  117. =item ret: ...
  118.  
  119. In SCALAR context returns the output filehandle of the spawned
  120. process.
  121.  
  122. In LIST context returns the input, outpur and error filehandles of the
  123. spawned process.
  124.  
  125. =item since: 1.99_15
  126.  
  127. =back
  128.  
  129. It's possible to pass environment variables as well, by calling:
  130.  
  131.   $r->subprocess_env->set($key => $value);
  132.  
  133. before spawning the subprocess.
  134.  
  135. There is an issue with reading from the read filehandle (C<$in_fh>)):
  136.  
  137. A pipe filehandle returned under perlio-disabled Perl needs to call
  138. select() if the other end is not fast enough to send the data, since
  139. the read is non-blocking.
  140.  
  141. A pipe filehandle returned under perlio-enabled Perl on the other hand
  142. does the select() internally, because it's really a filehandle opened
  143. via C<:APR> layer, which internally uses APR to communicate with the
  144. pipe. The way APR is implemented Perl's select() cannot be used with
  145. it (mainly because select() wants fileno() and APR is a crossplatform
  146. implementation which hides the internal datastructure).
  147.  
  148. Therefore to write a portable code, you want to use select for
  149. perlio-disabled Perl and do nothing for perlio-enabled Perl, hence you
  150. can use something similar to the C<read_data()> wrapper shown in the
  151. L<Synopsis|/Synopsis> section.
  152.  
  153. Several examples appear in the L<Synopsis|/Synopsis> section.
  154.  
  155.  
  156.  
  157. =head1 See Also
  158.  
  159. L<mod_perl 2.0 documentation|docs::2.0::index>.
  160.  
  161.  
  162.  
  163.  
  164. =head1 Copyright
  165.  
  166. mod_perl 2.0 and its core modules are copyrighted under
  167. The Apache Software License, Version 2.0.
  168.  
  169.  
  170.  
  171.  
  172. =head1 Authors
  173.  
  174. L<The mod_perl development team and numerous
  175. contributors|about::contributors::people>.
  176.  
  177. =cut
  178.