353 | | === Capturing Output === |
354 | | |
355 | | TODO |
| 353 | === I/O Redirection === |
| 354 | |
| 355 | mod_parrot can tie Parrot I/O operations on standard input and output to an Apache request, emulating CGI behavior. This is useful when you either don't want to expose the Apache API to the language, or the language lacks the features to support it (i.e. no objects). |
| 356 | |
| 357 | To tie a request to stdin or stdout, use the {{{stdin}}} and {{{stdout}}} methods of {{{ModParrot;Interpreter}}}. |
| 358 | |
| 359 | * {{{PMC stdin(PMC handle)}}} |
| 360 | * {{{PMC stdout(PMC handle)}}} |
| 361 | |
| 362 | {{{handle}}} is a PMC of one of the following types: |
| 363 | |
| 364 | * {{{ModParrot;Apache;RequestRec}}} - ties I/O on to the request |
| 365 | * {{{FileHandle}}} - assigns the filehandle object |
| 366 | |
| 367 | Each method returns the previous handle so you can restore it later. You must always restore stdin and stdout before returning from a metahandler. |
| 368 | |
| 369 | Here is code adapted from Pipp's response metahandler. It ties the request to stdin and stdout, runs the requested PHP script, and restores stdin and stdout to their original filehandles. |
| 370 | |
| 371 | {{{ |
| 372 | interp = ctx.'interp'() |
| 373 | php_file = r.'filename'() |
| 374 | oldout = interp.'stdout'(r) |
| 375 | oldin = interp.'stdin'(r) |
| 376 | r.'content_type'("text/html") |
| 377 | run_php_file(php_file) |
| 378 | interp.'stdout'(oldout) |
| 379 | interp.'stdin'(oldin) |
| 380 | }}} |