[libinline-java-perl] 327/398: ok
Jonas Smedegaard
dr at jones.dk
Thu Feb 26 11:43:19 UTC 2015
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.55
in repository libinline-java-perl.
commit 0dbb9324138a58f0f5bfe57fb86520fb44ab985d
Author: patrick_leb <>
Date: Thu Jun 17 00:33:21 2004 +0000
ok
---
Java/Callback.pod | 306 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 306 insertions(+)
diff --git a/Java/Callback.pod b/Java/Callback.pod
new file mode 100644
index 0000000..6228cf6
--- /dev/null
+++ b/Java/Callback.pod
@@ -0,0 +1,306 @@
+=head1 NAME
+
+Inline::Java::Callback - Callback into Perl from Java.
+
+=head1 SYNOPSIS
+
+=for comment
+
+ use Inline Java => <<'END' ;
+ 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() . "\n") ; # prints perl
+
+ sub perl {
+ return "perl" ;
+ }
+
+=for comment
+
+
+=head1 DESCRIPTION
+
+C<Inline::Java::Callback> allows you to call Perl functions from Java. To
+do this you need to create an C<org.perl.inline.java.InlinePerlCaller>
+object. Here is a example of a typical use:
+
+=for comment
+
+ use Inline Java => <<'END' ;
+ 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 . "\n") ; # prints 1
+
+ sub regexp {
+ my $target = shift ;
+ my $pattern = shift ;
+
+ return ($target =~ /$pattern/) ;
+ }
+
+=for comment
+
+
+=head1 CALLBACK API
+
+Here are the various methods that one can use to call into
+Perl:
+
+=over 4
+
+=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.
+
+ /* Example */
+ Integer sum = (Integer)CallPerlSub("main::add", new Object [] {new Integer(5), new Integer(3)}, Integer.class) ;
+
+=item public Object CallPerlStaticMethod(String pkg, String method,
+Object args[], Class cast)
+throws InlineJavaException, InlineJavaPerlException
+
+Calls the specified static package method (using the $pkg->$method()
+notation) with the supplied arguments and tries to create an object
+of type 'cast' with the result.
+
+ /* Example */
+ Integer sum = (Integer)CallPerlStaticMethod("main", "add", new Object [] {new Integer(5), new Integer(3)}, Integer.class) ;
+
+=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.
+
+ /* Example */
+ Integer sum = (Integer)eval("5 + 3", Integer.class) ;
+
+=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 C<require>
+function using the appropriate construct.
+
+ /* Example */
+ require("Someting")
+
+=item public Object require_file(String file)
+throws InlineJavaPerlException, InlineJavaException
+
+Requires the specified file.
+
+ /* Example */
+ require_file("./my_stuff.pl") ;
+
+=item public Object require_module(String module)
+throws InlineJavaPerlException, InlineJavaException
+
+Requires the specified module.
+
+ /* Example */
+ require_module("Data::Dumper") ;
+
+=back
+
+Note: For all CallPerl* and eval methods, the 'cast' parameter is optional
+and defaults to 'String.class'.
+
+These methods can throw 2 types of exceptions: C<InlineJavaException> and
+C<InlineJavaPerlException> (both of these belong to the C<org.perl.inline.java>
+package). The former designates an internal C<Inline::Java> error and the
+latter indicates that the Perl callback threw an exception (die() or croak()).
+The value of $@ (this can be a scalar or any valid "Inline::Java" object) can
+be retreived using the GetObject() method of the C<InlineJavaPerlException>
+object (if you are certain that $@ was a Perl scalar, you can use the
+GetString() method).
+ Z<>
+
+
+=head1 CALLBACK CONTEXT
+
+By default, callback are executed in scalar context. However if you want to
+call certain functions in list context, you must insert "@" in front of the
+function name. The result will then be passed on to Java as an Array:
+
+=for comment
+
+ use Inline Java => <<'END' ;
+ 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 ('a', 'b', 'c') ;
+ }
+
+ print(Pod_Context->get_list()->[1] . "\n") ; # prints b
+
+=for comment
+
+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 fiddicult to access for array
+types, the easiest way to do this is to create a dummy array of the desired
+type and call the getClass() method on that object (as seen above).
+ Z<>
+
+
+=head1 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, SWING events
+in Perl. Here's an example:
+
+=for comment
+
+ use Inline Java => <<'END' ;
+ 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('click!' . "\n") ; # prints click!
+ $b->StopCallbackLoop() ;
+ }
+
+=for comment
+
+The StartCallbackLoop method can be called on any InlineJavaPerlCaller 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 C<org.perl.inline.java.InlineJavaPerlCaller>
+object that has been created by that same thread.
+
+Also, only threads that communicate with Perl through C<Inline::Java> are allowed
+to create C<org.perl.inline.java.InlineJavaPerlCaller> objects and invoke their
+StartCallbackLoop / StopCallbackLoop methods.
+ Z<>
+
+
+=head1 SEE ALSO
+
+L<Inline::Java>, L<Inline::Java::PerlNatives>, L<Inline::Java::PerlInterpreter>.
+ Z<>
+
+
+=head1 AUTHOR
+
+Patrick LeBoutillier <patl at cpan.org> is the author of Inline::Java.
+
+Brian Ingerson <ingy at cpan.org> is the author of Inline.
+ Z<>
+
+
+=head1 COPYRIGHT
+
+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 http://www.perl.com/perl/misc/Artistic.html for more
+details.
+
+=cut
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libinline-java-perl.git
More information about the Pkg-perl-cvs-commits
mailing list