如何从Perl6调用Java方法 [英] How do I invoke a Java method from perl6
问题描述
use java::util::zip::CRC32:from<java>;
my $crc = CRC32.new();
for 'Hello, Java'.encode('utf-8') {
$crc.'method/update/(B)V'($_);
}
say $crc.getValue();
不幸的是,这不起作用
Method 'method/update/(B)V' not found for invocant of class 'java.util.zip.CRC32'
以下链接提供了此代码.这是我唯一能找到的例子
This code is available at the following links. It is the only example I've been able to find
- Rakudo Perl 6 on the JVM (slides)
- Perl 6 Advent Calendar: Day 03 – Rakudo Perl 6 on the JVM
推荐答案
最终答案
将下面的您的答案清理部分中说明的代码清理与下面的期望警报部分中提到的Pepe Schwarz的改进相结合,我们得到:
Final answer
Combining the code cleanups explained in the Your answer cleaned up section below with Pepe Schwarz's improvements mentioned in the Expectation alert section below we get:
use java::util::zip::CRC32:from<Java>;
my $crc = CRC32.new();
for 'Hello, Java'.encode('utf-8').list {
$crc.update($_);
}
say $crc.getValue();
您的答案已清理
use v6;
use java::util::zip::CRC32:from<Java>;
my $crc = CRC32.new();
for 'Hello, Java'.encode('utf-8').list { # Appended `.list`
$crc.'method/update/(I)V'($_);
}
say $crc.getValue();
一个重要的更改位是附加的.list
.
One important changed bit is the appended .list
.
'Hello, Java'.encode('utf-8')
片段返回一个对象,utf8
.该对象仅向for
语句返回一个值(本身).因此,for
仅迭代一次,将对象传递到其中包含update
行的代码块.
The 'Hello, Java'.encode('utf-8')
fragment returns an object, a utf8
. That object returns just one value (itself) to the for
statement. So the for
iterates just once, passing the object to the code block with the update
line in it.
如果update
行是.'method/update/([B)V'
,则仅进行一次迭代是有意义的,它映射到Java方法,该方法需要8位int的缓冲区,这实际上是Perl 6 utf8
的含义.但是,这需要一些支持Perl 6的代码(大概在核心编译器中)才能将Perl 6 utf8
编组(自动转换)为Java buf[]
,并且如果该代码曾经存在/正常工作,则确定何时无法正常工作.我用最新的Rakudo进行测试.
Iterating just once could make sense if the update
line was .'method/update/([B)V'
, which maps to a Java method that expects a buffer of 8 bit ints, which is essentially what a Perl 6 utf8
is. However, that would require some support Perl 6 code (presumably in the core compiler) to marshal (automagically convert) the Perl 6 utf8
into a Java buf[]
and if that code ever existed/worked it sure isn't working when I test with the latest Rakudo.
但是,如果有人在上面添加了明智的.list
并更改了代码块以使其匹配,那么事情就解决了.
But if one appends a judicious .list
as shown above and changes the code block to match, things work out.
首先,.list
导致for
语句遍历一系列整数.
First, the .list
results in the for
statement iterating over a series of integers.
第二,像您一样,我调用了Java方法的Integer arg版本(.'method/update/(I)V'
),而不是原始的缓冲区arg版本,然后代码可以正常工作. (这意味着从Perl 6 utf8
对象返回的无符号8位整数的二进制表示形式已经完全是Java方法所期望的,或者被自动地编组了.)
Second, like you, I called the Integer arg version of the Java method (.'method/update/(I)V'
) instead of the original buffer arg version and the code then worked correctly. (This means that the binary representation of the unsigned 8 bit integers returned from the Perl 6 utf8
object is either already exactly what the Java method expects or is automagically marshaled for you.)
另一个必要的更改是,根据您在下面的评论,from<java>
必须为from<Java>
-谢谢.
Another required change is that the from<java>
needs to be from<Java>
per your comment below -- thanks.
截至2015年1月:
-
仅将JVM后端用于Rakudo/NQP(即在JVM上运行纯P6代码)仍需要进一步加强,然后才能正式声明它可用于生产. (这是对整个P6生态系统有望在今年进行的全面强化的补充.)JVM后端有望在2015年实现这一目标-希望它将成为Perl 6首次正式启动的一部分今年的生产用途-但这很大程度上取决于需求,并且会有更多的开发人员使用它并提供补丁.
Merely using the JVM backend for Rakudo/NQP (i.e. running pure P6 code on a JVM) still needs more hardening before it can be officially declared ready for production use. (This is in addition to the all round hardening that the entire P6 ecosystem is expected to undergo this year.) The JVM backend will hopefully get there in 2015 -- it will hopefully be part of the initial official launch of Perl 6 being ready for production use this year -- but that's going to largely depend on demand and on there being more devs using it and contributing patches.
P6代码是一个其他项目.佩佩·施瓦茨(Pepe Schwarz)在过去的几个月里在提高速度,学习代码库和
P6 code calling Java code is an additional project. Pepe Schwarz has made great progress in the last couple months in getting up to speed, learning the codebase and landing commits. He has already implemented the obviously nicer shortname calling shown at the start of this answer and completed a lot more of the marshaling logic for converting between P6 and Java types and is actively soliciting feedback and requests for specific improvements.
这篇关于如何从Perl6调用Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!