Waba http://www.wabasoft.com/ - a subset of Java for Windows CE and PalmOS

It's embedded into web browsers (Netscape and MS Internet Explorer)

and more...

Java's JDBC is similar to Perl's DBI

Java has many different repositories of code. Efforts such as the Giant Java Tree http://www.gjt.org/ attempt to create a unified repository.

1.2 Opportunities to Combine Java and Perl

You have a Java program with a lot of data that needs to be parsed, filed, briefed, debriefed, and numbered.

You want to build your GUI in Java, but let Perl do the heavy lifting.

You've adopted the "Java is a systems language, Perl is a scripting language" paradigm, and it works for you.

You're not sure which regex implementation to use:

org.teeth.green.loony.raving.monster.regex.*;

com.zeppelin.regex.*;

You want the best of both worlds.

1.3 Important Differences between Java and Perl

perl compiles and executes programs each time you run them (unless you use the Perl compiler).

javac compiles programs in advance, java runs them in the Java interpreter.

The Java interpreter supports method overloading (methods can have the same name, but are differentiated on the basis of their argument types). Overloaded methods generally perform the same function, but methods with a shorter argument list often use defaults:

The Perl interpreter doesn't support method overloading. In JPL, when we call Java from Perl, we need to use some tricks to specify the Java method we want to invoke. We'll learn about this when we see JPL's getmeth function.

Note:

At the time this presentation was prepared, JPL did not work with Perl for Win32. However, JPL is in the core Perl distribution, and there are plans to make it work with Perl for Win32.

With that in mind, I'm presenting the JPL material first, because it is of interest to both Win32 and Unix Perl people. The Win32-specific stuff (alternatives to JPL) will come last. I won't be offended if the Unix people leave when I move to this section of the tutorial, since there is no Unix material in that section. I'm perfectly happy to take questions between JPL and ActiveX sections.

A subset of JPL now works on Win32. You can embed Java in Perl, but you cannot embed Perl in Java (yet).

2.1 JPL Overview

Let's look at an overview of JPL.

2.1.1 Calling Perl from Java

Well-supported by JPL, but it is a complicated process:

The JPL preprocessor parses the .jpl file and generates C code wrappers for Perl methods. It also generates Java and Perl source files.

The C compiler compiles the wrapper and links it to the libPerlInterpreter.so shared library, producing a shared library for the wrapper.

The Java compiler compiles the Java source file, which uses native methods to load the wrapper.

The wrapper connects the Java code to the Perl code in the Perl source file.

Fortunately, a generic Makefile.PL simplifies the process. This is a Perl script that generates a Makefile for you.

2.1.2 Calling Java from Perl

This works best when Perl is embedded within a Java program.

The JNI Perl module creates and loads a JVM. There is no precompiler, nothing extra -- it's just a Perl module and extension.

B<A Problem, Though>. In theory, you can call Java from standalone
Perl programs, but this doesn't work because some implementations
of Java use a user-level threads package (green threads) that
override some functions in the C library. Perl is comfortable
using these functions, but Java is not happy using the standard C
library functions.

Beware. If you issue the C<make> command and then run the examples
in your development directory, you might be in for a surprise! If
the JPL directories come first in your CLASSPATH and
LD_LIBRARY_PATH, you'll keep running the installed, older version,
rather than the one you are developing

Note:

"Source" means to load it into your current shell, with something like:

eval-backtick-setvars-backtick

as opposed to just executing it, because then only the subshell gets the environment vars.

2.3 Calling Perl from Java

Now, we'll look at how you can invoke Perl from Java.

2.3.1 Perl Methods

You can put Perl methods in your .jpl file. Perl methods are declared perl and use double curly braces to make life easier on the JPL preprocessor:

2.3.7 Summary: Calling Perl from Java

No need to pull arguments off of @_ with shift: JPL takes care of this for you. This includes $self.

If you pass a Java array into a Perl method, it comes in as a scalar reference.

Convert references to arrays of primitives with Get*ArrayElements

Use GetObjectArrayElement to get elements from arrays of strings and other objects.

To return an array from a perl method, declare the method as returning an array type, and either:

Return an array reference.

Return a string: JPL slices it up for you.

2.4 Calling Java from Perl

Next, let's look at how to invoke Java from Perl.

2.4.1 Java in Perl in Java

Remember the issues from 2.1.2 - this is unstable unless you are calling Java from Perl methods that are themselves embedded in a Java program.

2.4.2 Java in Perl: Simple Constructors

Use JPL::Class to load the class:

use JPL::Class "java::awt::Frame";

Invoke the constructor to create an instance of the class:

my $f = java::awt::Frame-new;>

You've got a reference to a Java object in $f, a Perl scalar. I think this is cool.

2.4.3 Constructors that Take Parameters

If the constructor has parameters, look up the method signature with getmeth:

my $new = getmeth("new", ['java.lang.String'], []);

The first argument to getmeth is the name of the method. The second argument is a reference to an array that contains a list of the argument types. The final argument to getmeth is a reference to an array containing a single element with the return type. Constructors always have a null (void) return type, even though they return an instance of an object.

Invoke the method through the variable you created:

my $f = java::awt::Frame->$new( "Frame Demo" );

Because Java supports method overloading, the only way Java can distinguish between different methods that have the same name is through the method signature. The getmeth function simply returns a mangled, Perl-friendly version of the signature. JPL's AutoLoader takes care of finding the right class.

For example, the method signature for $new is (Ljava/lang/String;)V. In Perl, this is translated to new__Ljava_lang_String_2__V. Sure, it means something to Java, but thanks to getmeth and JPL's AutoLoader, we don't have to worry about it!

2.4.4 More on getmeth

The getmeth function is not just for constructors. You'll use it to look up method signatures for any method that takes arguments.

To use getmeth, just supply the Java names of the types and objects in the argument or return value list. Here are a few examples:

Note. JPL creates these methods with the same name as the variable. You can also supply a value to set the variable's value. If you create a method with this name, it will collide with the one that JPL defines.