.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Callback 3pm" .TH Callback 3pm "2016-12-21" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Inline::Java::Callback \- Callback into Perl from Java. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Inline Java => <<\*(AqEND\*(Aq ; \& import org.perl.inline.java.* ; \& \& class Pod_caller extends InlineJavaPerlCaller { \& public Pod_caller() throws InlineJavaException { \& } \& \& public String perl() \& throws InlineJavaException, InlineJavaPerlException { \& \& return (String)CallPerlSub("main::perl", \& new Object [] {}) ; \& } \& } \& END \& \& my $pc = new Pod_caller() ; \& print($pc\->perl() . "\en") ; # prints perl \& \& sub perl { \& return "perl" ; \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`Inline::Java::Callback\*(C'\fR allows you to call Perl functions from Java. To do this you need to create an \f(CW\*(C`org.perl.inline.java.InlinePerlCaller\*(C'\fR object. Here is a example of a typical use: .PP .Vb 3 \& use Inline Java => <<\*(AqEND\*(Aq ; \& import java.util.* ; \& import org.perl.inline.java.* ; \& \& class Pod_regexp extends InlineJavaPerlCaller { \& public Pod_regexp() throws InlineJavaException { \& } \& \& public boolean match(String target, String pattern) \& throws InlineJavaException { \& try { \& String m = (String)CallPerlSub("main::regexp", \& new Object [] {target, pattern}) ; \& \& if (m.equals("1")){ \& return true ; \& } \& } \& catch (InlineJavaPerlException pe){ \& // $@ is in pe.GetObject() \& } \& \& return false ; \& } \& } \& END \& \& my $re = new Pod_regexp() ; \& my $match = $re\->match("Inline::Java", "^Inline") ; \& print($match . "\en") ; # prints 1 \& \& sub regexp { \& my $target = shift ; \& my $pattern = shift ; \& \& return ($target =~ /$pattern/) ; \& } .Ve .SH "CALLBACK API" .IX Header "CALLBACK API" Here are the various methods that one can use to call into Perl: .IP "public Object CallPerlSub(String sub, Object args[], Class cast) throws InlineJavaException, InlineJavaPerlException" 4 .IX Item "public Object CallPerlSub(String sub, Object args[], Class cast) throws InlineJavaException, InlineJavaPerlException" Calls the specified subroutine with the supplied arguments and tries to create an object of type 'cast' with the result. .Sp .Vb 2 \& /* Example */ \& Integer sum = (Integer)CallPerlSub("main::add", new Object [] {new Integer(5), new Integer(3)}, Integer.class) ; .Ve .IP "public Object CallPerlStaticMethod(String pkg, String method, Object args[], Class cast) throws InlineJavaException, InlineJavaPerlException" 4 .IX Item "public Object CallPerlStaticMethod(String pkg, String method, Object args[], Class cast) throws InlineJavaException, InlineJavaPerlException" Calls the specified static package method (using the \f(CW$pkg\fR\->$\fImethod()\fR notation) with the supplied arguments and tries to create an object of type 'cast' with the result. .Sp .Vb 2 \& /* Example */ \& Integer sum = (Integer)CallPerlStaticMethod("main", "add", new Object [] {new Integer(5), new Integer(3)}, Integer.class) ; .Ve .IP "public Object eval(String code, Class cast) throws InlineJavaPerlException, InlineJavaException" 4 .IX Item "public Object eval(String code, Class cast) throws InlineJavaPerlException, InlineJavaException" Evaluates the given Perl code and tries to create an object of type 'cast' with the result. .Sp .Vb 2 \& /* Example */ \& Integer sum = (Integer)eval("5 + 3", Integer.class) ; .Ve .IP "public Object require(String module_or_file) throws InlineJavaPerlException, InlineJavaException" 4 .IX Item "public Object require(String module_or_file) throws InlineJavaPerlException, InlineJavaException" Requires the specified module/file by using a heuristic (currently, checks whether or not the file exists) and calling Perl's \f(CW\*(C`require\*(C'\fR function using the appropriate construct. .Sp .Vb 2 \& /* Example */ \& require("Someting") .Ve .IP "public Object require_file(String file) throws InlineJavaPerlException, InlineJavaException" 4 .IX Item "public Object require_file(String file) throws InlineJavaPerlException, InlineJavaException" Requires the specified file. .Sp .Vb 2 \& /* Example */ \& require_file("./my_stuff.pl") ; .Ve .IP "public Object require_module(String module) throws InlineJavaPerlException, InlineJavaException" 4 .IX Item "public Object require_module(String module) throws InlineJavaPerlException, InlineJavaException" Requires the specified module. .Sp .Vb 2 \& /* Example */ \& require_module("Data::Dumper") ; .Ve .PP Note: For all CallPerl* and eval methods, the 'cast' parameter is optional and defaults to 'String.class'. .PP These methods can throw 2 types of exceptions: \f(CW\*(C`InlineJavaException\*(C'\fR and \&\f(CW\*(C`InlineJavaPerlException\*(C'\fR (both of these belong to the \f(CW\*(C`org.perl.inline.java\*(C'\fR package). The former designates an internal \f(CW\*(C`Inline::Java\*(C'\fR error and the latter indicates that the Perl callback threw an exception (\fIdie()\fR or \fIcroak()\fR). The value of $@ (this can be a scalar or any valid \*(L"Inline::Java\*(R" object) can be retrieved using the \fIGetObject()\fR method of the \f(CW\*(C`InlineJavaPerlException\*(C'\fR object (if you are certain that $@ was a Perl scalar, you can use the \&\fIGetString()\fR method). .SH "CALLBACK CONTEXT" .IX Header "CALLBACK CONTEXT" By default, callback are executed in scalar context. However if you want to call certain functions in list context, you must insert \*(L"@\*(R" in front of the function name. The result will then be passed on to Java as an Array: .PP .Vb 2 \& use Inline Java => <<\*(AqEND\*(Aq ; \& import org.perl.inline.java.* ; \& \& class Pod_Context { \& static private String dummy[] = {} ; \& \& static public String [] get_list() \& throws InlineJavaException, InlineJavaPerlException { \& InlineJavaPerlCaller pc = new InlineJavaPerlCaller() ; \& return (String [])pc.CallPerlSub("@main::list", \& null, dummy.getClass()) ; \& } \& } \& END \& \& sub list { \& return (\*(Aqa\*(Aq, \*(Aqb\*(Aq, \*(Aqc\*(Aq) ; \& } \& \& print(Pod_Context\->get_list()\->[1] . "\en") ; # prints b .Ve .PP Note: When calling a Perl function that returns a list or array, you will need to pass the Class object for the expected array type (in this case String []). Since these Class objects are difficult to access for array types, the easiest way to do this is to create a dummy array of the desired type and call the \fIgetClass()\fR method on that object (as seen above). .SH "CALLBACK LOOPS" .IX Header "CALLBACK LOOPS" It is now possible to use callbacks from different Java threads. One of the big advantages of this is that you can now handle, for example, \s-1SWING\s0 events in Perl. Here's an example: .PP .Vb 5 \& use Inline Java => <<\*(AqEND\*(Aq ; \& import java.util.* ; \& import org.perl.inline.java.* ; \& import javax.swing.* ; \& import java.awt.event.* ; \& \& class Pod_Button extends InlineJavaPerlCaller \& implements ActionListener { \& JFrame frame = null ; \& \& public Pod_Button() throws InlineJavaException { \& frame = new JFrame("Pod_Button") ; \& frame.setSize(100,100) ; \& JButton button = new JButton("Click Me!") ; \& frame.getContentPane().add(button) ; \& button.addActionListener(this) ; \& frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) ; \& frame.show() ; \& } \& \& public void actionPerformed(ActionEvent e){ \& try { \& CallPerlSub("main::button_pressed", new Object [] {}) ; \& } \& catch (InlineJavaPerlException pe){ \& // $@ is in pe.GetObject() \& } \& catch (InlineJavaException pe) { \& pe.printStackTrace() ; \& } \& } \& \& public void close(){ \& frame.dispose() ; \& frame.hide() ; \& frame = null ; \& } \& \& public void quit(){ \& System.exit(0) ; \& } \& } \& END \& \& my $b = new Pod_Button() ; \& $b\->StartCallbackLoop() ; \& $b\->close() ; \& \& # Maybe do some other stuff \& \& exit() ; # in client\-server mode, optional \& $b\->quit() ; # in JNI mode \& \& sub button_pressed { \& print(\*(Aqclick!\*(Aq . "\en") ; # prints click! \& $b\->StopCallbackLoop() ; \& } .Ve .PP The StartCallbackLoop method can be called on any \&\f(CW\*(C`org.perl.inline.java.InlineJavaPerlCaller\*(C'\fR object and will block the current thread and allow the reception of callbacks through any InlineJavaPerlCaller that has been created by the same (current) thread. The only way to interrupt such a StartCallbackLoop method is to call the StopCallbackLoop method on any \f(CW\*(C`org.perl.inline.java.InlineJavaPerlCaller\*(C'\fR object that has been created by that same thread. .PP Also, only threads that communicate with Perl through \f(CW\*(C`Inline::Java\*(C'\fR are allowed to create \f(CW\*(C`org.perl.inline.java.InlineJavaPerlCaller\*(C'\fR objects and invoke their StartCallbackLoop / StopCallbackLoop methods. .SH "SELECT-STYLE CALLBACK LOOPS" .IX Header "SELECT-STYLE CALLBACK LOOPS" The disadvantage with the type of callback loop presented in the previous section is that the main portion of the Perl program is completely blocked while waiting for callbacks. In version 0.51 a new \s-1API\s0 for callback loops was introduced, allowing for callbacks to be processed much in the same fashion one uses \fIselect\fR\|(2) to read data from a filehandle. Here's an example: .PP .Vb 5 \& use Inline Java => <<\*(AqEND\*(Aq ; \& import java.util.* ; \& import org.perl.inline.java.* ; \& import javax.swing.* ; \& import java.awt.event.* ; \& \& class Pod_Button extends InlineJavaPerlCaller \& implements ActionListener { \& JFrame frame = null ; \& \& public Pod_Button() throws InlineJavaException { \& frame = new JFrame("Pod_Button") ; \& frame.setSize(100,100) ; \& JButton button = new JButton("Click Me!") ; \& frame.getContentPane().add(button) ; \& button.addActionListener(this) ; \& frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) ; \& frame.show() ; \& } \& \& public void actionPerformed(ActionEvent e){ \& try { \& CallPerlSub("main::button_pressed", new Object [] {}) ; \& } \& catch (InlineJavaPerlException pe){ \& // $@ is in pe.GetObject() \& } \& catch (InlineJavaException pe) { \& pe.printStackTrace() ; \& } \& } \& \& public void close(){ \& frame.dispose() ; \& frame.hide() ; \& frame = null ; \& } \& \& public void quit(){ \& System.exit(0) ; \& } \& } \& END \& \& my $b = new Pod_Button() ; \& $b\->OpenCallbackStream() ; \& while ((my $rc = $b\->WaitForCallback(5)) > \-1){ \& if ($rc > 0){ \& # A callback is pending, we must process it. \& $b\->ProcessNextCallback() ; \& } \& else { \& # A timeout has occured after, in this case, 5 secs. \& print "5 seconds have passed, still waiting for callback...\en" ; \& # Maybe do some other stuff \& } \& } \& $b\->close() ; \& \& # Maybe do some other stuff \& \& exit() ; # in client\-server mode, optional \& $b\->quit() ; # in JNI mode \& \& sub button_pressed { \& print(\*(Aqclick!\*(Aq . "\en") ; # prints click! \& $b\->CloseCallbackStream() ; \& } .Ve .PP The StartCallbackStream method can be called on any InlineJavaPerlCaller object to initialize a channel to receive callbacks. The WaitForCallback method can then be called with a float timeout value (\-1 means wait forever, 0 means return immediately). The WaitForCallback method can return: .PP .Vb 3 \& rc > 0, indicating that rc callbacks are waiting to be processed \& rc == 0, indicating that a timeout has occured and no callbacks are waiting \& rc == \-1, indicating that the callback stream has been closed .Ve .PP The callback stream can be closed by calling CloseCallbackStream, which works similarly to the StopCallbackLoop method used in the previous section. .PP Also, the restrictions regarding thread communication stated in the previous section are valid in this case as well. .SH "SEE ALSO" .IX Header "SEE ALSO" Inline::Java, Inline::Java::PerlNatives, Inline::Java::PerlInterpreter. .SH "AUTHOR" .IX Header "AUTHOR" Patrick LeBoutillier is the author of Inline::Java. .PP Brian Ingerson is the author of Inline. .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (c) 2001\-2004, Patrick LeBoutillier. .PP 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 http://www.perl.com/perl/misc/Artistic.html for more details.