开发者

How do I redirect my STDOUT from HTTP::Server::Simple?

I'm using HTTP::Server::Simple::CGI to write a simple webserver. Within my handler, I have to run a process that prints something to STDOUT. I want the STDOUT redirected to the client and not to the console.

This is my handler

sub serve_content {
     $| =  undef;
     my 开发者_如何学编程$cgi  = shift;
     print $cgi->header('image/png');
     IPC::Run::run ['/usr/bin/myapp', '-o', '/dev/fd/3'], '3>&1', '1>&2', '>', *STDOUT;
 }

This prints the output of the app to the console, not to client who made the http request. How do I redirect it?


Order of redirections matter. Redirecting 3 to 1 and 1 to 2, efectivelly redirects 3 to 2. Try:

IPC::Run::run ['/usr/bin/myapp', '-o', '/dev/fd/3'], , '1>&2', '3>&1', '>', *STDOUT;

Or may be

IPC::Run::run ['/usr/bin/myapp', '-o', '/dev/fd/3'], , '1>&2', '3>', *STDOUT;


I wonder if you really need IPC::Run, I'm not convinced it gives you any real benefits.

Why not just open the script with a pipe in, read from the pipe in blocks, and send each block to the browser? Something like:

open my $pipe, '/usr/bin/myapp|';
binmode $pipe;
local $/ = \4096;
while(<$pipe>) {
    print;
}

If the image file is small enough to keep in memory at once, you could just as well use backticks.

 print `/usr/bin/myapp`;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜