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?

35 of 174 comments (clear)

  1. A bit extreme by Bogtha · · Score: 5, Insightful

    Why didn't you use the ImageMagick extension in PECL? 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.

    --
    Bogtha Bogtha Bogtha
    1. Re:A bit extreme by kv9 · · Score: 2, Interesting
      wow, it's when i see questions like these that i realize what kinda people code PHP and give it such a bad name. first of all, you could have just used the PECL extension as suggested above, which *will suffice*. or, in the worst case just exec() the binaries, like any normal person would do. not bringing Perl into the equation. what in the name of doctor frankenstein?

      </outraged>

    2. Re:A bit extreme by billcopc · · Score: 3, Funny

      <? bighair() { ?>
      Shot through the code,
      and you're too lame.
      You give PHP
      a bad name.
      <? } ?>

      --
      -Billco, Fnarg.com
  2. 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.

  3. Re:If you use PHP.... by Anonymous Coward · · Score: 2, Funny

    I can't see any possibility of flamewars in this subject.

  4. Re:If you use PHP.... by Anonymous Coward · · Score: 2, Insightful

    There is, alas, a reason. Much wider scope for takeup of your script. Plenty of ISPs offer a well configured PHP interpreter, but most do not offer a very wide or up to date CPAN selection.

  5. It's like peanut butter and chocolate... by heinousjay · · Score: 5, Funny

    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?

    --
    Slashdot - where whining about luck is the new way to make the world you want.
    1. Re:It's like peanut butter and chocolate... by Anonymous Coward · · Score: 4, Funny

      Dude #1: You got your Perl in my PHP!
      Dude #2: You got your PHP in my Perl!

      Announcer: Two great languages that only taste a little bit funny together. Now with less whitespace!!!

      Oh yeah, kids. Don't forget to try our other product, "Mac & PC". It's the cheesiest!

  6. Re:If you use PHP.... by drinkypoo · · Score: 3, Insightful

    Do most hosting providers provide the software necessary to embed perl in php? If not, this is a non-conversation. Mine doesn't even give me a compiler, I have to beg if I want anything (or compile a static binary somewhere else.) To be fair, they were plenty responsive the one time I asked for anything.

    --
    "You're right," Fisheye says. "I should have set it on 'whip' or 'chop.'"
  7. Hello there, Comrade Molotov! by mangu · · Score: 2, Funny

    Ahhh, nothing like a good flamewar! OK, I'll bite. Tell me, please, how do you write multidimensional arrays in Perl? No, no, you are *not* allowed to use references! Or what about a multidimensional hash? Suppose I wanted to migrate from PHP to Perl, how would I translate this perfectly valid PHP code to Perl:

    $x["a"]["b"]["c"] = "d";

    I mean, can you do it the Perl way, with less keystrokes?

    1. 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.

    2. 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
    3. Re:Hello there, Comrade Molotov! by nwbvt · · Score: 2, Insightful
      " Yes, the main reason for the existence of interpreted languages: simplicity."

      Translation: You are lazy.

      Come on, if your point was just that it is easier to work in PHP, you could have just compared the code needed to maintain multidimensional arrays in both languages. Instead you challenged Perl programmers to do something with only a subset of their languages features, implying (at least to young programmers visiting this thread and who do not know better) that such a feat was not possible with Perl and you need some sort of hack to get it to work. That is disingenuous at best. I want to see a valid technological reason why you would be required to develop a Perl app without using references.

      --
      Mathematics is made of 50 percent formulas, 50 percent proofs, and 50 percent imagination.
  8. 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.

  9. 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.
  10. Ruby on Rails by Anonymous Coward · · Score: 5, Funny

    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.

  11. 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
  12. Just use php's functions? by Xzzy · · Score: 3, Interesting

    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.

  13. Re:If you use PHP.... by Unnamed+Chickenheart · · Score: 3, Funny

    I am in no way an expert of programming nor web... ...but from my surfing experience, it seems like html + PHP creates very fast websides.

    (And PHP can do funky things like change colours on events like mouseover etc? groovy! )

    --
    urd
  14. 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.
  15. Re:If you use PHP.... by martinultima · · Score: 4, Interesting

    Bit off-topic, but if you need those kinds of features (compilers, custom Perl modules, etc.) maybe check out one of the user-mode Linux Web hosts like Linode.com – they've been running my site for a few months, and I'd say it's probably the best thing since Al Gore invented the Interwebs :-) You get your own distribution, your own choice of server configurations... it's like a dedicated server without the dedicated server. [Not trying to sound like an advertisement or anything like that, just a very happy user recommending a useful service.]

    --
    Creative misinterpretation is your friend.
  16. Re:If you use PHP.... by Anonymous Coward · · Score: 2, Funny

    Come now, you sound like every other expert PHP programmer to me.

  17. 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.

  18. Dear Slashdot, by edremy · · Score: 4, Funny
    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

    --
    "Seven Deadly Sins? I thought it was a to-do list!"
  19. 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
  20. Turck MMCache by rodent · · Score: 3, Interesting

    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.

    --
    rodent...
    Tactical nuclear weapons are a viable alternative!
  21. Congratulations by Anonymous Coward · · Score: 2, Funny

    Congratulations! You found the worst of both worlds.

  22. 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.

  23. Haven't you ever heard of that Loverboy song? by trentfoley · · Score: 2, Funny

    "Pearl and PHP DNA... just don't mix"

  24. 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. :-)

  25. 7 languages in one program by hallvar · · Score: 2, Funny

    http://blog.sykosopp.com/2006/02/26/one-file-to-ru le-them-all/

    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.

    --
    Hallvar Helleseth (hallvar)
  26. 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.
  27. Re:If you use PHP.... by nwbvt · · Score: 2, Insightful
    I'm not saying the language is completely useless (ok, I guess that was what I said in my last post, but that was intended to be a joke). I'm sure it is very useful for html writers who want to make a simple dynamic web page without learning how to program. The problem is that it teaches those people some very bad habits if they want to go into 'real' computer science.

    For instance, accessing variables that are not defined returns nothing by default (most languages will throw an exception should this happen). This results in problems like this occuring.

    There are plenty of easy to learn languages such as Python and Ruby out there that do not have nearly as many problems, so there is not too much of an excuse to use PHP for anything but the simplest web page. What this guy is trying to do, use PHP despite the fact that it is obviously not powerful enough for his requirements), is just wrong.

    --
    Mathematics is made of 50 percent formulas, 50 percent proofs, and 50 percent imagination.
  28. 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
  29. Re:If you use PHP.... by budgenator · · Score: 2, Insightful

    There is a php application that I've written and am still writing, and I really wish I had gone the Perl route in stead. In PHP when you hit the wall, you hit it fast and hard. The most infurating thing is when they changed default settings which broke a lot of helper libs, which were abadoned by the developers. I'm with you and now I'm converting my app to Perl and CPAN modules; no sense in developing my app, and debugging other peoples libs.

    --
    Apocalypse Cancelled, Sorry, No Ticket Refunds