“E” adds pipes
The “E” editor for Windows adds support for in/out pipes in today’s release:
cat records.txt | sort | e | process_records
The “E” editor for Windows adds support for in/out pipes in today’s release:
cat records.txt | sort | e | process_records
I’m working on extending Textpattern with some scripts I developed for Blosxom. Textpattern is a Php-based weblogging tool, and my scripts are all Perl-based plugins and command-line utilities. I don’t really want to port the scripts to Php, so I decided to find a way to call Perl from Php.
In a few minutes of searching, I only found one Perl binding for Php. It isn’t considered stable, and it isn’t available from my web host (Dreamhost). I did find an answer in the Php manual, but a googling on the specific topic of calling Perl from Php came up dry. So I decided to make it a bit more obvious.
All of my scrips are simple text processors, so the inputs and outputs can be passed using the stdin/stdout pipes. Using both the input and output pipes of a process is a bit more than the standard Php exec functions can handle, so we’ll be using the proc function family. Nearly every language has a set of these functions (and that’s a good thing).
You can download the demo script here. The script will need to be executable, it needs the correct path to the Perl interpreter, and the log file folder needs to be writable by the web-server process.
The Perl script reads text from stdin, and replaces spaces with underscores. From Php, we can call the Perl script and manage the standard in, out, and error pipes. The example is slightly modified from the Php manual.
The basic process:
The PHP script:
< ?php $handles = array( // 1 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("file", "test-errors.txt", "a") // stderr ); $test_text = "This is a test"; $process = proc_open("./test.pl", $handles, $pipes); if (is_resource($process)) { fwrite($pipes[0], "$test_text"); fclose($pipes[0]); while (!feof($pipes[1])) { $output .= fgets($pipes[1], 1024); } fclose($pipes[1]); $r = proc_close($process); echo "Before: $test_text<br />"; echo "After : $outputn<br />"; } ?>>
And the perl script:
#!/usr/local/bin/perl
# replace spaces with _s in stdin
while(<>) {
s/ /_/g;
print;
}>