/usr/lib/perl5/Inline/Java/Callback.pod is in libinline-java-perl 0.53-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | =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 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 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
C<org.perl.inline.java.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 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 API for callback loops
was introduced, allowing for callbacks to be processed much in the same
fashion one uses select(2) to read data from a filehandle. 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->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...\n" ;
# 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('click!' . "\n") ; # prints click!
$b->CloseCallbackStream() ;
}
=for comment
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:
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
The callback stream can be closed by calling CloseCallbackStream, which works
similarly to the StopCallbackLoop method used in the previous section.
Also, the restrictions regarding thread communication stated in the previous
section are valid in this case as well.
Z<>
=head1 SEE ALSO
L<Inline::Java>, L<Inline::Java::PerlNatives>, L<Inline::Java::PerlInterpreter>.
Z<>
=head1 AUTHOR
Patrick LeBoutillier <patl@cpan.org> is the author of Inline::Java.
Brian Ingerson <ingy@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
|