Java Reference
In-Depth Information
Marrying Java and Perl
Problem
You want to call Java from Perl.
Solution
To call Java from Perl, use the Perl Inline::Java module. To go the other way—calling
Perl from Java—use javax.script , as in Calling Other Languages via javax.script .
Discussion
Perl is often called a “glue language” that can be used to bring together diverse parts of the
software world. But, in addition, it is a full-blown language in its own right for creating soft-
ware. A wealth of extension modules provide ready-to-run solutions for quite diverse prob-
lems, and most of these modules are available free from CPAN, the Comprehensive Perl
Archive Network . Also, as a scripting language, it is ideally suited for rapid prototyping. On
the other hand, although building graphical user interfaces is definitely possible in Perl, it is
not exactly one of the language's strengths. So you might want to construct your GUI using
Java Swing, and, at the same time, reuse business logic implemented in Perl.
Fortunately, among the many CPAN modules, Inline::Java makes the integration of Perl
and Java a breeze. Let's assume first that you want to call into Java from Perl. For business
logic, I have picked a CPAN module that measures the similarity of two strings (the so-called
Levenshtein edit distance ). Example 24-8 shows the complete source. You need at least ver-
sion 0.44 of the module Inline::Java ; previous versions did not support threaded applica-
tions properly, so use of Swing wasn't possible.
Using the module this way requires that the Java source be included in the Perl script with
special delimiters, as shown in Example 24-8 .
Example 24-8. Swinging.pl
#! /usr/bin/perl
# Calling Java from Perl
use strict;
use warnings;
Search WWH ::




Custom Search