Slashdot Mirror


PHP and Perl in One Script?

gbulmash asks: "Recently, I began working on a graphics project and wanted to use ImageMagick. As a PHP coder, I figured I'd use MagickWand for PHP. But after some investigation, I decided that an alpha at 0.1.8 with sparse documentation just wasn't going to be good enough for production use. I decided that PerlMagick would be a much better API, but I didn't want to code the whole project in Perl. In the end, I found a cool package for embedding Perl code in PHP scripts (with an article on its use) and it went to a 1.0.0 release, earlier this year. I think I've found my answer, but before I make a final decision and go ahead with it, I thought I'd ask the knowledgeable Slashdot crowd: Is there a better way of interfacing Perl with PHP so you can get the best of both worlds?" So you've got Perl in your PHP, is there a way to do PHP in your Perl?

13 of 174 comments (clear)

  1. If you want to use Perl, use Perl! by Anonymous Coward · · Score: 5, Informative

    Instead of going with some bizarre rube-goldberg hack like this (does it even work with dynamically loadable Perl modules?), just write a separate Perl script that does what you need and receives data on standard input and gives responses on standard output. Use PHP's proc_open() function. You can even set up the Perl script as a daemon, if you like, and get higher performance.

    I used this same trick to interface a binary-only executable to my PHP script. You can even use XML to pass the data back and forth, if you swing that way. I don't know how this might work on Windows, if that's what you're running on.

    By the way, version numbers mean absolutely nothing except the author's opinion of his own software. In fact, the article you posted says: "the extension is still marked EXPERIMENTAL". Sounds like alpha to me.

    Not to mention, this is the same guy that wrote Turck MMCache and in my testing, mmcache couldn't stay stable for more than a day or two, and would randomly return junk characters instead of output from our PHP scripts. Not exactly an author I'd trust for this kind of lower-level stuff. (Yeah, I know, there are 10 billion geeks who used MMCache on their 5-hit-per-day blog and it worked perfectly, and will not hesitate to point this out. It just didn't work for me, and other products did, sorry!)

    For what it's worth, I've used "pecl-imagick-0.9.11" on my Gentoo servers to manipulate images (basically, to generate thumbnails by resizing and sharpening) and it works perfectly. I think this is different than what you're talking about. So unless you really just can't live without Perl (?), try that first.

    Instead of looking at version numbers, test thoroughly.

  2. CPAN! by schweini · · Score: 3, Informative

    as almost always, CPAN is your friend in all things perl-related.

    aside from that, i really don't understand why you'd use PHP if you're familiar enough with perl to use it.

  3. Re:If you use PHP.... by powerlord · · Score: 3, Informative

    As Iunderstand it ... Mason, a perl based web authoring kit has been used by quite a few web sites, including Amazon.

    Yeah ... I guess their website is just painfully slow and prone to problems.

    --
    This space for rent. All reasonable inquiries will be entertained at proprietors discretion.
  4. Re:If you use PHP.... by Soong · · Score: 3, Informative

    Huh, some perl driven web apps:
    http://www.slashcode.com/
    http://scoop.kuro5hin.org/

    I have been trolled. Hope this helps. Have a nice day.

    Personally I prefer Java Servlets, with perl a second place, then python, then bash, then C, then php.

    --
    Start Running Better Polls
  5. Re:Hello there, Comrade Molotov! by iwrigley · · Score: 5, Informative

    Erm... how about

    $x{'a'}{'b'}{'c'} = 'd';

    Yes, it uses references, but if you're scared of them, pretend it's a 'real' multidimensional hash.

  6. ImageMagick? by martinultima · · Score: 3, Informative

    I tend to use ImageMagick a lot via PHP scripts, but not with any sort of polyglot of code or anything – I just use exec("convert ... ");. It's probably not the most cross-platform way, but since I tend to do exclusively Linux/UNIX-based stuff, and most of the code is only used by me for my own sites anyway, it's not a problem at least as far as I'm concerned.

    --
    Creative misinterpretation is your friend.
  7. Re:Hello there, Comrade Molotov! by novas007 · · Score: 2, Informative

    $y[0][0][0] = 0;
    $x{a}{b}{c} = 'd';

    Beat you by 6 characters on that second one. And if I'm not allowed to use references, you're not allowed to use strings. Perl is designed with references in mind.

    --
    To smash a single atom, all mankind was intent / Now any day the atom may return the compliment
  8. Re:Incidentally... by chromatic · · Score: 2, Informative
    Because array is just an entry in a symbol table...

    Whoa, not for lexicals.

    ... so [] and {} both deference AND lookup, all the time...

    Nope, only after you've already dereferenced one level.

  9. You shure you're a PHP developer? by Qbertino · · Score: 2, Informative

    There's like ten bazillion ways to integrate PHP with Image processing tools like Image Magick just like with any other OSS language. Only more so because the PHP userbase is largest. No need to use some Hack involving Perl. Typo3 (www.typo3.org) uses Image Processing to generate Menubuttons and stuff, there are countless other PHP projects out there doing the same, and it's no sweat at all to start the imagemagick CLI tools (contvert, etc.) including parameters from PHP. 30 lines of code max I'd say. Do you homework and then ask in some PHP forums and mailinglists for sample code. You'll get answers in no time.

    --
    We suffer more in our imagination than in reality. - Seneca
  10. Why not use http as your connector? by joelwyland · · Score: 5, Informative

    Why do you need them to work in the same script? Have your PHP generate an img tag that references the perl script in the src attribute. Then the web browser will fetch the image data directly from the perl script and you don't need a Frankensteinian monster.

  11. GD? by corychristison · · Score: 2, Informative
    May I ask why not GD? It's pretty mature and very usable in PHP. Most installations around the globe include GD2 so would it not be a more viable solution [especially if it's an open source project]?

    Just wondering. :-)

  12. Re:I've used both PHP and Perl. Advice: Don't do i by KiloByte · · Score: 2, Informative

    Using too many languages is bad?

    Hell, then a page of mine that uses all of:
    * bash
    * C
    * C++
    * Perl
    * perhaps even Python or some such (hell, I don't look under the pants of programs I call)
    * sed
    * grep

    And as far as I know, this is not a rare practice. If it's not a CPU-critical script, bash makes for good glue code. And you can't call it hard to maintain (at least until you look at sourceless configure scripts).

    --
    The creatures outside looked from Alt-Right to Antifa; but already it was impossible to say which was which.
  13. Calling PHP from Perl by Dalfiatach · · Score: 3, Informative
    So you've got Perl in your PHP, is there a way to do PHP in your Perl?

    I am in the joyous position of having to maintain a monstrosity of an application that was written by at least 4 different programmers over a period of years with numerous kludges plastered on top of a highly dodgy initial design.

    A lot of the early code was in Perl. Obfuscated, unreadable, indecipherable (and uncommented) Perl. So I decided to re-implement whole libraries of functions in PHP instead. But...a lot of what goes on in the application is driven by 2 Perl daemons, and they needed access to the new PHP libraries too.

    So:

    use LWP::UserAgent;
    my $ua = LWP::UserAgent->new;
    $ua->agent("PerlApplication/0.1 ");

    #construct the url (including GET params)
    my $varstring = "http://localhost/perlcalls.php";

    # Create a request
    my $req = HTTP::Request->new(POST => $varstring);
    $req->content_type('application/x-www-form-urlenco ded');
    $req->content('');

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);
    print "PHPresult: " . $res->content;
    # Check the outcome of the response
    if ($res->is_success) {

    #parse content of response
    if($res->content eq "OK"){
    print "PHP returned OK";
    }

    }
    else {
    #system failure, PHP unreachable
    print "PHP Failure!";

    }
    --
    Day by day, the penguins steal my sanity