Final answer
Combining the code cleanup described in the Orderly Response section below with the Pepe Schwartz enhancements mentioned in the Waiting warnings 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();
Your answer has been cleared.
use v6; use java::util::zip::CRC32:from<Java>; my $crc = CRC32.new(); for 'Hello, Java'.encode('utf-8').list {
One important bit changed is the added .list
.
Fragment 'Hello, Java'.encode('utf-8')
returns an object, a utf8
. This object returns only one value (itself) in the for
statement. Thus, for
iterates only once, passing the object to the code block using the update
line.
Iteration only made sense once if the update
line was .'method/update/([B)V'
, which maps to a Java method that expects an 8-bit ints buffer, which is essentially what Perl 6 utf8
. However, this will require some Perl 6 support code (presumably in the main compiler) to marshal (automatically convert) Perl 6 utf8
to Java buf[]
, and if that code ever existed / worked, it certainly doesn't work. when I test the last Rakudo.
But if you add a reasonable .list
, as shown above, and change the code block to match, everything works.
First, .list
causes the for statement to execute, iterating over a whole series of integers.
Secondly, like you, I named the Integer arg version of the Java method ( .'method/update/(I)V'
) instead of the original version of arg arg and the code worked correctly. (This means that the binary representation of the unsigned 8-bit integers returned from the Perl 6 utf8
object either already exactly matches the Java method or is automatically marshaled for you.)
Another required change is that from<java>
should be from<java>
for your comment below - thanks.
Waiting Warning
As of January 2015:
Simple use of the JVM server for Rakudo / NQP (i.e., running pure P6 code on the JVM) still requires more hardening before it can be officially declared ready for use. (This is in addition to the comprehensive hardening that is expected to take place throughout the year of the entire P6 ecosystem.) The JVM backend, I hope, will arrive there in 2015 - it will hopefully become part of the initial official launch of Perl 6, ready for production this year - but it will largely depend on demand and on the fact that there will be more developers using it and making corrections.
P6 code code invoking Java code is an optional project. Pepe Schwartz has made great strides in the last couple of months, reaching speed by studying the code base and