In Line 25, you need to fill in the correct settings for your MSSQL
server installation. You also need to have a PHP installation with
support for MSSQL. This is usually done by passing the switch
-with-mssql during the compilation of PHP. Some distributions also
require you to install FreeTDS, which is used by PHP to access MSSQL.

Now, let's review some specific portions of the code.
To use the PHP::Interpreter in your code, declare its use, as
in line 1.
To create a new instance of PHP interpreter, do as is shown in line 5:

my $php = PHP::Interpreter->new;

As with object-oriented programming, you now can invoke methods on the $php
object to achieve interoperability with PHP. The above code shows two
functions provided by the PHP::Interpreter for interoperability. In line
24,
we are calling the include() function, which includes a PHP script
file to the environment, and you can call functions defined in the file
natively from the object. We do the same with writeIP in line 25, which is a PHP
function declared in writems.php on line 2 of the writems.php listing.
The Eval
function of the $php object allows you to execute a specific PHP
instruction, as with a live interpreter. The instruction is interpreted,
and the return may be stored into a variable or used directly, as in
line 26. As you can see in the above program, PHP::Interpreter provides
an object-oriented mechanism for completely integrating the two
languages. This integration is achieved with only two lines of code: the
initial use statement and the instantiation of the object.
PHP::Interpreter is not only about calling functions and procedural
programming, it also works with object-oriented PHP. This is how you
can instantiate an object of class defined in a PHP:

my $instance = $PHP->instantiate('PHPclass', @args);

The instance is stored in $instance, and any arguments are passed to
the class' constructor.

Example 2: Integrating Perl with PHP

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:

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

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.

Let's look at this piece of code in more detail. In line 4 of the PHP
program, 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:

I'm using a xampp installation under Windows. Sadly, I'm restricted to using a Windows environment. Is there any way to install the PHP::Interpretor in this environment? When running the Makefile.pl, I always get the error php-config not found. I don't quite understand what to do here. Maybe someone could help me? Thank you very much :-)

Trending Topics

Webinar: 8 Signs You’re Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th

Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.