Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Perl and PHP

by vihnny (Initiate)
on Dec 10, 2012 at 19:25 UTC ( #1008154=perlquestion: print w/ replies, xml ) Need Help??
vihnny has asked for the wisdom of the Perl Monks concerning the following question:

Morning guys, I have a quick question for you! How do I integrate PErl and php together?? I mean, how do I call perl subroutines in php? Example

<?php $variable = perl_Subroutine; echo "<p>The result is </p> . $variable"; ?> #!/usr/bin/perl #################### sub perl_Subroutine{ #################### my $self = shift; (if I passing something to this function/method o +f course). my $result; $result = $self + 2; return $result; }

Comment on Perl and PHP
Download Code
Re: Perl and PHP
by runrig (Abbot) on Dec 10, 2012 at 19:39 UTC
    One possibility is the exec function...another is to ditch PHP and just do it in Perl.
      Not correct
Re: Perl and PHP
by CountZero (Chancellor) on Dec 10, 2012 at 20:07 UTC
    PHP is actually at heart only some kind of templating system where you can include PHP-code into your HTML code.

    If you like that concept, why not use a Perl-templating system in your web-server?

    Template Toolkit can even run pure Perl-code in the web-page.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Thanks, I will for sure look into it. However, I will be able to actually create my DB class with perl and communicate with the CGI module? What I wanted to do is create my pages in php and use some perl for functions to handle the Dababase structure. For me, perl is really good with hashes ref. Php, not so much..

        Mojolicious::Lite!!! If you really want to do most of your work in the templates (as is done in a typical PHP script), you can do that here too. But it really supports (and encourages) a more healthy separation of concerns. Take a look at the docs and see if it's a good fit.


        Dave

        Running PHP and Perl in one webpage will surely degrade the performance of the webserver. For each call to a Perl-routine from PHP, the web-server will have to start a new instance of Perl. Multiply that with the number of pages that can get simultaneously accessed and the performance crawls down to a halt.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Perl and PHP
by tobyink (Abbot) on Dec 10, 2012 at 22:32 UTC

    An embedded Perl interpreter for PHP does exist but it's a pretty bizarro thing to do.

    Update: the following works here:

    <?php $perl = new Perl(); $perl->eval(<<<'PERL' sub add_two { my $n = shift; return $n + 2; } PERL ); echo "add_two(3) is ... ", $perl->add_two(3), "\n";

    Interestingly when running print "$^X $]" in the embedded Perl, I get: /usr/bin/php 5.010001 (that version number happens to correspond to my operating system's pre-packaged version of Perl).

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Perl and PHP
by ww (Bishop) on Dec 11, 2012 at 01:09 UTC
    "how do I call perl subroutines in php"

    Rather than asking here in the Monastery, you might want to check with the folks in the PHPouthouse?

    Seriously, you really should consider the suggestions from runrig, CountZero and davido, three Monks of great wisdom. And tobyink's "bizarro" observation is, IMO, well justified by the fact that PHP's limitations ^H^H^H^H^H advantages (other than the fact that you may have some familiarity) are slim to none.

Re: Perl and PHP
by Anonymous Monk on Dec 11, 2012 at 01:46 UTC
    Why screw the whole thing up in two languages, when you could merely screw it up in one? Step back ... far back ... from this requirement. Take your hands off the keyboard and put them firmly in your pockets. Now ... look. I mean, really look. What are you trying to do here? How many existing web-sites on this blue planet are even remotely similar to what you have in mind? Go find one, find out how they did it, and do as they did. Exactly as they did.
Re: Perl and PHP
by LanX (Abbot) on Dec 11, 2012 at 01:59 UTC
    PHP? What's that?

    lanx@ubuntu:~$ perldoc php No documentation found for "php". lanx@ubuntu:~$ perldoc PHP No documentation found for "PHP". lanx@ubuntu:~$ man php No manual entry for php lanx@ubuntu:~$ man PHP No manual entry for PHP lanx@ubuntu:~$ man -k php lanx@ubuntu:~$ man -k PHP

    UPDATE:

    Most probably a typo... please consider activating "strict" and "warnings".

Re: Perl and PHP
by TJPride (Pilgrim) on Dec 11, 2012 at 11:21 UTC
    Mixing the two is not probably something you should do. Yes, PHP is weak when it comes to manipulating nested data structures, however the difficulty and general messiness of adding Perl to your PHP makes that approach rather abortive. I'd personally either keep the two separate and use an AJAX (Javascript) pull from your Perl script to populate data on your PHP page, or do the page entirely in Perl. Or depending on your needs, you could substitute a properly-designed MySQL query for data manipulation in either language. What precisely are you trying to do?

    I use both Perl and PHP for work, incidently - Perl for complicated data processing and PHP for more presentation-oriented sites. I try not to mix the two much in the same site, though.

      PHP's advantage is its disadvantage: it's a great big wad. Everybody's got the same wad. You can't add anything to it with modules; everything is compiled-in. But everyone's got the same wad ... oops! ... until a new PHP version comes along and changes the language. And heaven help you if you add a single unwanted space before or after the bracket tags. Bleah.

        Actually there's plenty of modules for PHP; many of them are distributed as part of the PHP core. For example, the cURL extension. Compile PHP with the --with-curl option and you get cURL; otherwise you don't.

        The difference is that PHP extensions tend to vomit their entire API into the global namespace.

        The embedded Perl interpreter for PHP I mentioned earlier is a compiled PHP extension module. It happens to be one that is not bundled with PHP itself, and also a rare example of an object-oriented one, which manages to avoid polluting the global namespace much.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008154]
Approved by Jim
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-04-16 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (417 votes), past polls