The biggest advantage of Perl/PHP integration is PHP's ability to access Perl CPAN modules. There are CPAN modules for almost everything that can be done via software; you can use PHP::Interpreter in PHP to call CPAN modules to extend a PHP application to do anything, which is not native to PHP—for example, it enables you to write to IO ports. Writing to IO ports has been the exclusive domain of C/C++ programs, but with PHP::Interpreter, even a mere scripting language can have the capability to write to IO ports. The example that follows shows how to use Perl code with PHP, but first, we discuss the features of PHP::Interpreter that allow PHP/Perl integration.
The PHP interpreter, invoked via PHP::Interpreter, has a special class that allows PHP to Perl communication. Create an instance of the class via this call in PHP:
1. <?php
2. $perl = Perl::getInstance();
3. ?>
The new $perl object allows you to evaluate specific Perl instructions in PHP, such as:
1. <?php
2. $perl = Perl::getInstance();
3. $perl->eval(q^
4. print "Executing Perl code in PHP\n";
5. ^);
6. ?>
Similar to Example 1, where we called a PHP function in Perl, you can call Perl subroutines in PHP. All subroutines defined in the Perl program, which instantiated the PHP::Interpreter instance, can be invoked like this (I will provide a more detailed example shortly):
1. <?php
2. $perl = Perl::getInstance();
3. $return = $perl->call('sub', @args);
4. ?>
And, of course, you can get and set variables from the Perl file that instantiated the PHP::Interpreter; however, only package variables, not lexical variables, are supported.
Let's look at a practical application of PHP/Perl integration—for example, a snippet of Perl code that uses the Babel Fish CPAN module. (Babel Fish is a piece of software that allows you to translate text between different languages. To learn more about Babel Fish, go to babel.altavista.com.) The PHP program calls the translate function, which will be implemented in Perl, to translate a string in English to German and retrieve the output.
To install the Babel Fish CPAN, go to search.cpan.org/CPAN/authors/id/D/DM/DMUEY/AltaVista-BabelFish-v42.0.1.tar.gz, and install it with the standard installation procedure, as shown previously in this article.
AltaVista::BabelFish also has some prerequisites, such as Class::Std and Class::Std::Util. These need to be downloaded and installed for Babel Fish to work:
1. use AltaVista::BabelFish;
2. use PHP::Interpreter;
3. my $p = PHP::Interpreter->new();
4. $p->include("phpscript.php");
5. my $val = $p->invoke();
6. sub translate
7. {
8. my $phish = AltaVista::BabelFish->new({ source => $_[0], target =>
$_[1] });
9. return $phish->translate($_[2]) or die $phish->get_errstr();
10. }
The phpscript.php file contains the following:
1. <?php
2. function invoke()
3. {
4. $perl = Perl::getInstance();
5. $string = $perl->call('translate', 'en','de','Translate this for me');
6. print "Translated string: $string\n";|
7. }
8. ?>
we are creating an instance of the Perl class using Perl::getInstance(). This is the special class inserted by the PHP::Interpreter dynamically into the environment to achieve PHP to Perl integration.
In line 5, we then use the class object, $perl, to invoke a function called translate, which is defined in line 6 of the Perl program, and we pass the arguments accordingly. The subroutine translate is invoked from the Perl script, and the translation is done via the Babel Fish module. The translated string is returned to PHP and printed via the print statement. Although this is a rudimentary example, the entire script can be extended to provide runtime translation for viewers of a dynamic Web page generated from PHP. With CPAN and the PHP::Interpreter, the possibilities of what can be achieved in PHP are bounded only by the developer's imagination.
You can use the PHP Perl class for object-oriented Perl as well. Invoke a Perl object via the new() function, as follows:
1. <?php
2. $perl = Perl::getInstance();
3. $instance = $perl->new('perlclass', @args);
4. ?>
The first argument to the new() method, in line 3, is the name of the class, and additional arguments are passed to the constructor of the class.