[]RSS

[ Here: About | Archives+Tags | Artwork | Resumé | Contact ] [ Elsewhere: Comic | Projects | Philosophy | Work ]

Calling Perl from Php

[Comment]

May 20th, 2004 in Howto. Perl

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.

Pipe dreams

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:

  1. Define what to do with the process’s pipes. Notice that the error pipe is mapped to a log file (which is appended on each call).
  2. Define some text to test with the Perl script.
  3. Open the process, which is our Perl script. This will fail if the script can’t be found, or if it isn’t executable.
  4. Write the test text to the input pipe of the Perl script.
  5. Read the output of the Perl script.
  6. Close the process. This is best done after all of the pipes are closed (otherwise it causes deadlock).

The PHP script:

< ?php
$handles = array(                             // 
   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;
}

Assimilating Aggregation

[Comment]

May 15th, 2004 in Tools. Weblog

I’ve been quietly changing my web-browsing habits over the last few months. I now use , a free (as in beer) web-based RSS aggregator for browsing news, weblogs, link logs, and finding random intelligence. So far I’ve avoided reading stuff via RSS, mostly because I find that aggregators provide a monochromatic view of the web.

Combined with the incredible random-access link services provided by Google, I now view the web almost exclusively in aggregate form. Where I used to visit dozens of sites daily for news and tech bits, I visit two. It’s simpler, more efficient, and it lets me spend less time reading (yet read more of what I want to).

style news, comment, and link aggregators are now old-school, as better information is available directly from the source. Insider news, commentary, and meta-commentary is now readily accessible right from the people who are part of the actual events, instead of through the indirection and filtering of news editors.

We are now our own news editors, writers, and publishers. Not only are the levels of indirection reduced, a few layers of prejudice are removed. It’s a set of Freedoms that I didn’t really see coming out of the net, and I’m not sure how it will play out. But the result is incredible, and I like it.

CSS and XHTML: The Non-Standards

[Comment]

May 2nd, 2004 in Rants. Weblog

Today I gave up on web standards. Again.

I’m in the middle of re-tooling the weblog, one of those bi-annual habits that plagues me. This particular round was spurred by an interesting , a fairly sensible (and seemingly liberated) php/mysql-based weblogging tool. I figured would make a good replacement for my currently functional, but somewhat tedious, publishing .

I decided to make another honest attempt to find XHTML/CSS Zen. I’ve been previously frustrated by inconsistent browser support for DIV positioning, which appears simple on the surface (but ends up a time-sink for getting layouts to mesh). Since then, I’ve read several articles outlining the ‘right’ hacks for getting DIVs and CSS to render sensibly across all browsers (cough IE), and thought that this time would be easier.

Getting the site layouts to work in Mozilla and Konqueror-based browsers was easy: both are capable, compliant browsers. CSS and XHTML are a bit limiting (compared to postscript or latex), but I can live with that. It’s Internet Explorer that causes all of the problems.

Even after applying the many well-known CSS hacks, I was still suffering rendering problems. Then I realized: this is a simple problem, why am I wasting my time trying to get a two-column layout to display properly? Layout is a trivial problem, and I’ve spent hours trying to approximate a simple format in a handful of browsers. What a waste of energy.

The standards are broken. IE is broken. Trying to make the standards work by hacking and slashing the markup is broken. Trying to make /my/ site standards compliant is broken. The whole approach stinks.

All I want is a simple way to layout text. I’m not a moron, I’ve used dozens of markup systems for more than 15 years. HTML 1.0 worked, even though it was a bit limiting. XHTML works, as long as you avoid the zealotry. It just doesn’t make sense to hack around a bogus rendering model, and shitty support for an end result that can be achieved through simpler means. Why are we wasting our time with standards-purity?

The whole approach lacks of elegance and balance. It’s broken. It’s a time-sink. And, it takes away from the time we spend thinking and writing about things.