Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror

PHP and Perl in One Script? 174

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?
This discussion has been archived. No new comments can be posted.

PHP and Perl in One Script?

Comments Filter:
  • A bit extreme (Score:5, Insightful)

    by Bogtha ( 906264 ) on Friday June 23, 2006 @07:29PM (#15593365)

    Why didn't you use the ImageMagick extension in PECL [php.net]? There's plenty of image processing options with PHP, just because MagickWand didn't work out for you, that doesn't mean you have to concoct a monstrous hybrid of PHPerl.

  • by agent dero ( 680753 ) on Friday June 23, 2006 @07:32PM (#15593373) Homepage
    I thought this was already publicized, Perl & PHP in one script? Isn't that just a brain fuck [muppetlabs.com] :)
  • by Anonymous Coward on Friday June 23, 2006 @07:33PM (#15593377)
    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.
    • Turck MMCache (Score:3, Interesting)

      by rodent ( 550 ) on Friday June 23, 2006 @11:32PM (#15594517)
      I have to say I used Turck MMCache on an 80 server farm serving up >4 million page loads (not hits) per day with not a single problem. This was back in the PHP4 days. Based on the load on the machines it could have easily been done with 5 but management wanted to spread the risk in an insanely large manner.
  • by heinousjay ( 683506 ) on Friday June 23, 2006 @07:50PM (#15593467) Journal
    All the security of PHP and all the readability of Perl? It's a surefire win. I wonder why the whole world hasn't caught on to this one yet?
  • CPAN! (Score:3, Informative)

    by schweini ( 607711 ) on Friday June 23, 2006 @08:09PM (#15593588)
    as almost always, CPAN is your friend [cpan.org] 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.
    • by VGPowerlord ( 621254 ) on Saturday June 24, 2006 @03:15AM (#15595153)
      1. Perl using standard CGI is slower than PHP's mod_php
      2. Perl using mod_perl is more complicated than PHP's mod_php

      mod_perl really is a big issue. In PHP, something that runs under two identical setups, one that uses mod_php and the other that uses PHP CGI will run identically.

      mod_perl's caching and function wrapping can lead to unexpected results with global variables and modules (See: perldoc FindBin).

  • by Anonymous Coward on Friday June 23, 2006 @08:13PM (#15593609)
    I'm probably going to get flamed for this but have you considered using rails? Seriously, you just spend a couple of hours installing it on your development box before you discover the speed and power of rails. Then when you've used the programming equivilent of a lumphammer to sculpt your fine detailed script, you get to spend the next week looking for hosting. I do all my development on a gentoo box, it only took me a weekend to set up from stage 1. I have some great USE flags that make my RAILS development go super fast and gentoo only takes about 1.5 hours a week to maintain. Well, sometimes it takes a little longer because the QA is non-existent of late.

    That's what I'd do anyway, RoR running on gentoo with some kicking USE flags. I'm developing a complete Ajax OS in my spare time using this system. It's going to revolutionize the OS market and yes, it will have use flags.
    • by Poromenos1 ( 830658 ) on Friday June 23, 2006 @08:33PM (#15593698) Homepage
      I'm writing a processor in RoR, wanna cooperate? :P
    • by horatio ( 127595 ) on Friday June 23, 2006 @11:00PM (#15594399)
      you get to spend the next week looking for hosting

      dreamhost [dreamhost.com], which has RoR hosting :)
  • by Xaroth ( 67516 ) on Friday June 23, 2006 @08:18PM (#15593637) Homepage
    So you've got Perl in your PHP, is there a way to do PHP in your Perl?

    Sure! [perl.org]

    </joke>

  • by Xzzy ( 111297 ) <sether@tr u 7 h . org> on Friday June 23, 2006 @08:29PM (#15593683) Homepage
    I know ImageMagik is the kitchen sink of image editing, but have you looked into PHP's embedded image functions? There's very few effects you couldn't produce on your own with those functions. I'll grant it's probably easier to just pass arguments to the ImageMagik library, but probably not more efficient.

    Allows you to avoid the problems of calling ImageMagik, piping it through perl, then doing whatever you need to do in PHP. Sounds like a recipie for excessive server load, to me.
  • ImageMagick? (Score:4, Informative)

    by martinultima ( 832468 ) <martinultima@gmail.com> on Friday June 23, 2006 @08:42PM (#15593747) Homepage Journal
    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.
  • by edremy ( 36408 ) on Friday June 23, 2006 @10:05PM (#15594118) Journal
    I am trying to write a question that will get the maximum number of snarky replies. Taco didn't post my "Advice for converting a Ruby on Rails application to ASP.NET?" submission, so do you have another suggestion?

    Thank you
    A. T. Roll

  • by Qbertino ( 265505 ) <moiraNO@SPAMmodparlor.com> on Friday June 23, 2006 @10:21PM (#15594214)
    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.
  • by MobileTatsu-NJG ( 946591 ) on Friday June 23, 2006 @10:40PM (#15594311)
    This question isn't all that relevent to the topic other than a brief mention of PHP. No hard feelings if modded off-topic.

    I'm a 3D artist working in the VFX industry. Recently, I've been scripting within my 3D app. (The syntax of this language is a lot like PHP...) There are a lot of things you can do with the scripting portion of the app. You can write modeling plugins that play with the geometry. You can write shaders. You can automate tedious tasks. You can work on the RGB buffers after they've been written. Etc. There was a rumor flying around that they were going to switch from the PHP'esque syntax to Python, the justification being that they can do much more powerful things with it. I'm unfamiliar with Python, so please forgive my ignorance. I just wanted to ask: Can anybody describe in laymen's terms what this sort of change could mean towards writing better/more efficient code? I'm almost afraid to ask this question for fear of starting a PHP vs. Python war, but I'm just curious if there's a significant difference in philosophy with this language or if the company making this app is just trying to follow in the footsteps of more successful apps that already use Python.

    • by MikeFM ( 12491 ) on Saturday June 24, 2006 @09:05PM (#15598522) Homepage Journal
      Python can be something of a mind-trip if you've only used languages with a C-styled syntax such as PHP. The majority of the difference is in the lack of containing braces around blocks which is replaced by making whitespace meaningful. It works pretty well once you get used to it. Python is a terse language that doesn't make you spell out every little detail, such as Java or C/C++, and is strongly object oriented. I find Python easy to write and maintain for anything from a short script up to full-blown applications. For the most part it's a very clean well designed language.

      For short scripts it'd probably not mean much for you to switch from PHP (or a similar language) but for larger scripts the clean syntax and object oriented nature would save you a lot of work. The kicker being that you need to understand the concept of objects to work well in Python.
    • by SimHacker ( 180785 ) * on Sunday June 25, 2006 @01:05AM (#15599198) Homepage Journal

      Good languages are hard to design. Scriptable applications that dream up their own special-purpose scripting languages tend to have a lot of deep flaws and fundamental limitations. Most of the developer's resources go into the main application, not designing and supporting the scripting language. And special purposes non-standard languages usually don't have good development and debugging tools, either.

      Python is a very well designed language, with a huge active community and robust collection of tools and extension modules, which was designed to be used as a general purpose application scripting language, from day one. Its syntax is quite simple, and there aren't a lot of exceptions to the rules (like Perl) or subtle nuances and inexplicable quirks (like PHP), so it's quite easy to learn.

      One of the big upsides to using Python as an application scripting language is that all of the standard and add-on Python modules are at your disposal, and if you need to do anything specialized, you can write your own Python modules (using SWIG [swig.org] makes that quite easy).

      Why would you want to plug your own custom Python modules into a scripable application like Maya? You might want to call libraries that import and export content, and integrate existing i/o and previewing code from the applications that consume the content you're building in Maya. And if your other applications are also scripted in Python, then you can use the same code and modules in your application and your tools, which really rocks! Python on Windows also has full support for OLE Automation, so you can easily integrate applications like Excel, and ActiveX controls, too.

      But to stay on topic: Despite its crystal-meth-like popularity, PHP is an ameteurishly designed language, and has a long track record of each new version breaking compatibility by attempting to paper-machete over stupid mistakes of the past, that shouldn't have been made in the first place. But the difference between PHP and Perl, is that PHP is just naive, and didn't know it was making those mistakes. But Perl is totally malevolent, it chose to make all those mistakes on purpose, and it revels in its own fractal complexity.

      Sure, go ahead with your experiment to arrange a shotgun marriage of PHP and Perl. But beware: this could happen to you [theinquirer.net]!

      -Don

  • by bfree ( 113420 ) on Friday June 23, 2006 @11:36PM (#15594529)
    How about Catalyst-View-PHP [cpan.org].
  • by Anonymous Coward on Friday June 23, 2006 @11:42PM (#15594556)
    Congratulations! You found the worst of both worlds.
  • by joelwyland ( 984685 ) on Saturday June 24, 2006 @01:24AM (#15594896)
    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.
  • by trentfoley ( 226635 ) on Saturday June 24, 2006 @02:01AM (#15595001) Homepage Journal
    "Pearl and PHP DNA... just don't mix"
  • GD? (Score:2, Informative)

    by corychristison ( 951993 ) on Saturday June 24, 2006 @03:35AM (#15595196)
    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. :-)

    • by Phil John ( 576633 ) <phil@noSpaM.webstarsltd.com> on Saturday June 24, 2006 @05:19AM (#15595378)

      GD didn't support GIF files (legally) for a long time (you could always compile it in, but that meant nothing to people on shared hosting). Also, ImageMagick produced, in my opinion, better quality files at smaller sizes than GD used to.

      Of course, today, with no GIF problems and the quality on both about par, there's not a lot of difference.

      Our system offers a plugin based solution, use GD or ImageMagick with a single config file change. As far as calling ImageMagick from PHP goes, we just use the command execution functions.

  • by mrjb ( 547783 ) on Saturday June 24, 2006 @05:40AM (#15595422)
    I've used both PHP and Perl for web development. I'd discourage mixing PHP and Perl in the same script, it's not necessary. Sure, it can be done, but just because it can be done doesn't necessarily make it a good idea. It will make your website harder to maintain, because whoever is going to maintain your website in the future will need to know both perl and PHP (granted, they are relatively similar languages).

    There is a clearly defined functional separation in this case, though. It would seem to me that you want to do your web stuff in PHP and your images in Perl. Write your image scripts in perl then; There's nothing wrong with generating in PHP. Perl people will be able to maintain your image scripts, PHP people will be able to maintain your web scripts.
  • by hallvar ( 21994 ) on Saturday June 24, 2006 @06:38AM (#15595507) Homepage
    http://blog.sykosopp.com/2006/02/26/one-file-to-ru le-them-all/ [sykosopp.com]

    Embed all your languages, this file can be interpreted or compiled in 7 different coding languages:

    $ php poly.sh.pl.php.tcl.cpp.bf.py.c.lhs.txt
    #I'm a PHP script
    $ python poly.sh.pl.php.tcl.cpp.bf.py.c.lhs.txt
    I'm a Python program.
    $ perl poly.sh.pl.php.tcl.cpp.bf.py.c.lhs.txt
    I'm a Perl program.
    $ tclsh poly.sh.pl.php.tcl.cpp.bf.py.c.lhs.txt
    I'm a tcl script.
    $ sh poly.sh.pl.php.tcl.cpp.bf.py.c.lhs.txt
    I'm a sh script.

    Rename file to compile in C/C++:

    $ gcc p.c -o c;./c
    I'm a C program (C89 with // comments, trigraphs disabled).
    $ g++ p.c -o cp;./cp
    I'm a C++ program, trigraphs disabled.
  • by Dalfiatach ( 984736 ) on Saturday June 24, 2006 @09:50AM (#15596001)
    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!";

    }
  • #!/bin/sh
    php <<EOF
    <?php
    echo htmlspecialchars("Hello world!\n");
    EOF

    perl <<EOF
    print "Hello world!\n";
    EOF
  • Well you could use the PHthon PHP/Python bridge, then the Perlthon Python/Perl bridge.

    ...But what's the point of writing everything in one file but different languages? Why not write a pure PHP script that calls a pure Perl script directly? Or are you just looking for a way to avoid using more than one file, by making your application much more complex? Are they paying you by the hour, instead of for results, efficiency, simplicity, robustness, etc?

    Wasn't Parrot supposed to solve all these problems? Or was that just a sick joke that some misguided people too much too seriously?

    -Don

  • by Zaphod2016 ( 971897 ) on Sunday June 25, 2006 @02:20PM (#15601314) Homepage

    For your consideration: http://us3.php.net/manual/en/ref.image.php [php.net]

    If you would like to see a simple script which generates a graph on the fly, help yourself to this [zaphodforpresident.com].

    Yes, there are languages far more powerful than PHP. But, as I say EVERY TIME this flamewar goes down- are you sure you need them? As my grand-pappy used to say: why go deer hunting with a tank when a shotgun will do just fine?

DISCLAIMER: Use of this advanced computing technology does not imply an endorsement of Western industrial civilization.

Working...