So, here is a first rare module: Apache::SubProcess

Doug wrote this module a while ago, but it was never mentioned here on the
list. It's available from CPAN.

The output of C<system()>, C<exec()>, and C<open(PIPE,"|program")> calls
will not be sent to the browser unless your Perl was configured with
C<sfio>.

One workaround is to use backticks:

  print `command here`;

But a cleaner solution is provided by the C<Apache::SubProcess>
module. It overrides the exec() and system() calls, with ones that
work correctly under mod_perl.

Let's see a few examples:

  use strict;
  use Apache::SubProcess qw(system exec);
  
  my $r = shift;
  $r->send_http_header('text/plain');
  
  # override built-in system() function
  system "/bin/echo hi there";
  
  # send an output of a program
  my $efh = $r->spawn_child(\&env);
  $r->send_fd($efh);
  
  # pass arguments to a program and sends its output
  my $fh = $r->spawn_child(\&banner);
  $r->send_fd($fh);
  
  # pipe data to a program and send its output
  use vars qw($String);
  $String = "hello world";
  my($out, $in, $err) = $r->spawn_child(\&echo);
  print $out $String;
  $r->send_fd($in);
  
  # override built-in exec() function
  exec "/usr/bin/cal"; 
  
  print "NOT REACHED\n";
  
  sub env {
      my $r = shift;
      #$r->subprocess_env->clear;
      $r->subprocess_env(HELLO => 'world');
      $r->filename("/bin/env");
      $r->call_exec;
  }
  
  sub banner {
      my $r = shift;
      # /usr/games/banner on many Unices
      $r->filename("/usr/bin/banner"); 
      $r->args("-w40+Hello%20World");  
      $r->call_exec;
  }
  
  sub echo {
      my $r = shift;
      $r->subprocess_env(CONTENT_LENGTH => length $String);
      $r->filename("/tmp/pecho");
      $r->call_exec;
  }
  # where /tmp/pecho is:
  # --------------------
  #!/usr/bin/perl 
  #read STDIN, $buf, $ENV{CONTENT_LENGTH}; 
  #print "STDIN: `$buf' ($ENV{CONTENT_LENGTH})\n";


Enjoy!

_______________________________________________________________________
Stas Bekman    mailto:[EMAIL PROTECTED]      http://www.stason.org/stas
Perl,CGI,Apache,Linux,Web,Java,PC     http://www.stason.org/stas/TULARC
perl.apache.org    modperl.sourcegarden.org   perlmonth.com    perl.org
single o-> + single o-+ = singlesheaven    http://www.singlesheaven.com

Reply via email to