The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


Inline::Java::PerlNatives - Map Java native methods to Perl functions.


   use Inline Java => <<'END' ;
      import* ;

      class Pod_PN extends InlineJavaPerlNatives {
         public Pod_PN() throws InlineJavaException {

         native public String hello() ;

   package Pod_PN ;
   sub hello {
      return "hi!" ;

   package main ;
   my $b = new Pod_PN() ;
   print($b->hello() . "\n") ; # prints hi!


WARNING: Inline::Java::PerlNatives is still experimental.

Inline::Java::PerlNatives allows you to define your callbacks as native Java methods that are automatically linked to Perl subroutines. You implement the Perl subroutine directly in the package in which Inline::Java binds your class. You can do this by making your Java code extend the class.

Note: PerlNatives requires J2SDK version >= 1.4


Let's revisit an example from the Inline::Java::Callback documentation:

   use Inline Java => <<'END' ;
      import java.util.* ;
      import* ;
      import javax.swing.* ;
      import java.awt.event.* ;

      class Pod_Button_PN extends InlineJavaPerlNatives
                          implements ActionListener {
         public Pod_Button_PN() throws InlineJavaException {
            JFrame frame = new JFrame("Pod_Button") ;
            frame.setSize(100,100) ;
            JButton button = new JButton("Click Me!") ;
            frame.getContentPane().add(button) ;
            button.addActionListener(this) ;

         public void actionPerformed(ActionEvent e){
            button_pressed() ;

         native public void button_pressed() ;

   package Pod_Button_PN ;
   sub button_pressed {
      print('click!' . "\n") ; # prints click!
      $main::b->StopCallbackLoop() ;

   package main ;
   $main::b = new Pod_Button_PN() ;
   $main::b->StartCallbackLoop() ;

Extending InlineJavaPerlNatives tells Inline::Java that all native methods declared in that class should be linked to Perl subroutines implemented in the approriate package. You can then call these methods from Java just like regular methods. You can even call them from Perl if they are public.


Inline::Java::PerlNatives has a few limits that one must be aware of:

  1. You cannot declare 2 native methods with the same name in a class (even if they have different signatures).

  2. Native methods can have arguments of any type, but they must return either void or an Object (use wrappers like Integer and Double to return primitive types).

  3. Even if you do not declare them, InlineJavaException and InlineJavaPerlException exceptions (as well as others) may be thrown from within the native methods


Inline::Java, Inline::Java::Callback, Inline::Java::PerlInterpreter.


Patrick LeBoutillier <> is the author of Inline::Java.


Copyright (c) 2001-2004, Patrick LeBoutillier.

All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the terms of the Perl Artistic License. See for more details.