Permalink
..
Failed to load latest commit information.
Calendar.pl Perlito5 - java - process annotations at parse-time Jun 1, 2016
GameClient.pl Added socket based client-server game Feb 26, 2016
GameServer.pl Added socket based client-server game Feb 26, 2016
GuiHelloWorld.pl Perlito5 - java - example GUI application Jul 28, 2016
GuiHelloWorld2.pl Perlito5 - java - example GUI application Jul 28, 2016
HBaseGet.pl Perlito5 - docs - discourage indirect object notation in Perlito5-Jav… Dec 22, 2015
HBaseScan.pl Perlito5 - docs - discourage indirect object notation in Perlito5-Jav… Dec 22, 2015
HelloWorldApp.java java - use anon class for Closure Nov 9, 2011
Http.pl Perlito5 - java - "http server" example wip Apr 28, 2016
README Perlito5 - cpan version 9.004 Oct 30, 2015
Sample.java Perlito5 - java - tests for @perl_array = java_native[] Jul 30, 2015
Test.pl Perlito5 - java - "Class" are singletons Dec 16, 2016
TestConcurrent.pl Perlito5 - docs - discourage indirect object notation in Perlito5-Jav… Dec 22, 2015
TestJS.pl Perlito5 - java - update calling-JavaScript example Feb 2, 2017
TestThread.pl Perlito5 - docs - discourage indirect object notation in Perlito5-Jav… Dec 22, 2015
TestThread2.pl Perlito5 - java - AtomicInteger example Apr 28, 2016
TypedIterator.pl Perlito5 - java - update native-Java example Feb 2, 2017
Types.pl Perlito5 - java - native values Apr 28, 2016
Types2.pl Updated TODO list. Removed todo's from code example. Apr 28, 2016
code_too_large.pl Perlito5 - java - TODO - JVM code-too-large error Jul 28, 2015
depgraph.pl dependancy script update Dec 31, 2015
deps.svg Dependency graph of generated java classes Dec 31, 2015
jrun Perlito5 - Java - cleanup Mar 1, 2016

README

"Perlito to-Java" Examples

    This document is a collection of examples and imortant
    notes about Perlito to-Java compiler usage.


General Information:

    The main motivation of to-Java compilation is to be
    able to use native java APIs with Perl5 syntax.
    The perl code is compiled to java code. The supported
    Perl syntax is a compatible subset of Perl5.

    That model of operation implies two major tradeoffs:

        CON: Some Perl5 modules will not compile. Specifically,
             this applies to XS modules, modules dependent on
             XS modules and modules which use syntax constructs
             outside of supported subset.

        PRO: All the java classes ever written can be used with
             the supported subset of Perl syntax (which is large
             enough for writting useful code).


How to run:

  One-liners
  
    perl perlito5.pl -Isrc5/lib -I. -It -Cjava -e ' my $v; my $x = 3; $v = sub { $x = $x + 1; $_[0] + $x;  }; say $v->(123), " ", $x;  ' > Main.java ; javac Main.java ; java Main


  Perl scripts

    perl perlito5.pl -Isrc5/lib -I. -It -Cjava myscript.pl  > Main.java ; javac Main.java ; java Main


Example 1: (basic operations: file => ./benchmark.pl):

    This code does not have any dependencies and will
    run in both Perl5 and Java-compiled version:

        my $count;

        my $i = 0;
        while ( $i < 400 ) {
            my $j = 0;
            while ( $j < 400 ) {
                my $k = 0;
                while ( $k < 400 ) {
                    $k = $k + 1;
                    $count = $count + 1;
                }
                $j = $j + 1;
            }
            $i = $i + 1;
        }

        print "done $count\n";

    This is fairly simple benchmark. In our runs java
    was ~3 times faster.


Example 2: (Using regexes: file => benchmark_regex.pl)

    This is a simple regex benchmark:

        my $count;
        my $i = 0;
        while ( $i < 400 ) {
            my $j = 0;
            while ( $j < 400 ) {
                my $k = 0;
                while ( $k < 400 ) {
                    $k = $k + 1;
                    $count = $count + 1 if $k =~ /42/;
                }
                $j = $j + 1;
            }
            $i = $i + 1;
        }
        print "done $count\n";


    In our benchmarks, the java code and the perl code
    performed roughly the same. This is not surprising
    since even though Java is a compiled language, Perl
    is made for text processing and its regex engine is
    writen in highly optimized C code.



Example 3: (HBase Get example: file => HBaseGet.pl)

    This example show how to create java object and call java
    methods.

        package IOException { import => "java.io.IOException" };
        package Configuration { import => "org.apache.hadoop.conf.Configuration" };
        package HBaseConfiguration { import => "org.apache.hadoop.hbase.HBaseConfiguration" };
        package Get { import => "org.apache.hadoop.hbase.client.Get" };
        package HTable { import => "org.apache.hadoop.hbase.client.HTable" };
        package Result { import => "org.apache.hadoop.hbase.client.Result" };
        package Bytes { import => "org.apache.hadoop.hbase.util.Bytes" };

        # Instantiating Configuration class
        my Configuration $config = HBaseConfiguration->create();

        # Instantiating HTable class
        my HTable $table = new HTable( $config, "prices" );

        # Instantiating Get class
        my Get $g = new Get( Bytes->toBytes("1234") );

        # Reading the data
        my Result $result = $table->get($g);

        # Reading values from Result class object
        my $max_price = Bytes->toString( $result->getValue( Bytes->toBytes("d"), Bytes->toBytes("max_price") ) );

        # Printing the values
        say "max price: " . $max_price;


Example 4: (HBase Scanner example: file => HBaseScanner.pl)

    This example follows up on example 3 with the addition of casting java
    objects back to perl objects.

        package IOException { import => "java.io.IOException" };
        package Configuration { import => "org.apache.hadoop.conf.Configuration" };
        package HBaseConfiguration { import => "org.apache.hadoop.hbase.HBaseConfiguration" };
        package Get { import => "org.apache.hadoop.hbase.client.Get" };
        package HTable { import => "org.apache.hadoop.hbase.client.HTable" };
        package Result { import => "org.apache.hadoop.hbase.client.Result" };
        package Bytes { import => "org.apache.hadoop.hbase.util.Bytes" };
        package Scan { import => 'org.apache.hadoop.hbase.client.Scan' };
        package ResultScanner { import => 'org.apache.hadoop.hbase.client.ResultScanner' };

        # Instantiating Configuration class
        my Configuration $config = HBaseConfiguration->create();

        # Instantiating HTable class
        my HTable $table = new HTable( $config, "prices" );

        my Scan $scan = new Scan();

        $scan->setStartRow(Bytes->toBytes("100002303"));
        $scan->setStopRow(Bytes->toBytes("100005303"));
        $scan->addColumn(Bytes->toBytes("d"), Bytes->toBytes("max_price"));

        my ResultScanner $resultScanner = $table->getScanner($scan);

        # Not declared as Result since we want perl object
        my $scan_result = $resultScanner->next();

        while (defined $scan_result) {

            # cast perl object to java object
            my Result $java_obj_result =
                $scan_result->to_Result();

            # TODO: autoamatic casting (my Result $java_obj = $scan_result;)
            my $scanned_max_price =
                Bytes->toString(
                    $java_obj_result->getValue(
                         Bytes->toBytes("d"),
                         Bytes->toBytes("max_price")
                     )
                )
            ;

            say "max_price $scanned_max_price";

            $scan_result = $resultScanner->next();
        }


    Casting from java to perl is currently manual. In the future this
    will be automatic, like:


        my Result $java_obj = $scan_result;