<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Perlblogs &#187; java</title>
	<atom:link href="http://perlblogs.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://perlblogs.com</link>
	<description>Posts from selected Perl bloggers</description>
	<lastBuildDate>Mon, 06 Feb 2012 20:47:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>The Verifiability of Syntax</title>
		<link>http://www.modernperlbooks.com/mt/2011/04/the-verifiability-of-syntax.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/04/the-verifiability-of-syntax.html#comments</comments>
		<pubDate>Fri, 08 Apr 2011 15:55:25 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[catalyst]]></category>
		<category><![CDATA[dancer]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[webprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Imagine you're building a web application. Imagine that the application exposes several URLs. Imagine that the application's workflow requires navigation through those URLs. You have to develop and deploy a model of those URLs somehow. The well-worn CGI approach is...]]></description>
			<content:encoded><![CDATA[
        <p>Imagine you're building a web application. Imagine that the application exposes several URLs. Imagine that the application's workflow requires navigation through those URLs.</p>

<p>You have to develop and deploy a model of those URLs somehow.</p>

<p>The well-worn CGI approach is to write separate semi-interdependent programs for each step of the application. Each new URL element is a separate program. (This model matches the statelessness of HTTP nicely, but requires careful thought on the server side to manage the necessary statefulness securely.</p>

<p>Much of the web has settled on an MVC approach where all requests come in
through one or more controllers which dispatch to functions or methods based on
request contents: the URI, any form parameters, and the query string. Often a
framework handles this dispatch by unpacking all of this information and
matching regular expressions or fraguments of the URI, then traversing all the
way to an endpoint.</p>

<p>I like <a href="http://catalystframework.org/">Catalyst</a>'s approach to
dispatch (see especially <a
href="http://search.cpan.org/perldoc?Catalyst::DispatchType::Chained">Catalyst's
chained dispatch</a>, a feature I'm pleased to see <a
href="http://perldancer.org/">Dancer</a> adopting as well): dispatch points are
methods on a controller object with metadata attached in the form of subroutine
attributes. This metadata registers methods as dispatch points and identifies
the pieces of the URI they consume and any necessary preconditions.</p>

<p>It's flexible and powerful. Once you understand the terms used in the Catalyst world, you have many options to do the right thing for your application. You also get some form of validation because subroutine attributes are merely Perl 5 syntax, not entirely raw string text (though admittedly, the parameters to these attributes are raw strings which require validation&mdash;fortunately, as Perl 5 processes attributes during compilation, the attribute handlers can sanitize and validate these parameters to some degree during compilation).</p>

<p>Another approach, used in many places including the Java framework which
shall remain nameless, is to use an external file to manage routing
information. In this case, it's an XML file which maps URI components to
controller classes and methods on those controllers. It also maps the results
of those methods to template bundles (declared in another XML file).</p>

<p>Thanks to <a href="http://www.modernperlbooks.com/mt/2011/04/beans-versus-immutable-objects.html">the Java web world's tragicomic affair with Beans</a> and the prevalence of CamelCase, I constantly get capitalization <em>wrong</em> in this file. You see, while I may have a controller class named <code>AdminAction</code>, <em>the right way</em> to instantiate classes in Java is to let <a href="http://www.springsource.org/">Spring</a> manage their lookup and instantiation and lifecycle.</p>

<p>In other words, even though I <em>know</em> that this application needs a
class named <code>AdminAction</code> and that that class will always be a
controller and that the routing information declared in this XML file relies on
<code>AdminAction</code> behaving as coded, <em>the right way</em> to write
this application means declaring a bean named <code>adminAction</code> (notice
the capitalization) in yet another XML file and using <em>that</em> in the
routing XML file instead of the class name.</p>

<p>(Apparently this makes replacing the real controller with a mock controller
easier for testing, as if testing a mock controller were worth my
time&mdash;what could it tell me about the validity of my Spring configuration
or my routing configuration that basic end-to-end testing couldn't tell me
better? And people who like to write tests wonder why some people complain that
a focus on unit testing is, in many cases, useless busy work.)</p>

<p>The funny thing about the bean syntax is that it means, at its core, that if
you have an object with instance data such as <code>companyName</code> or
<code>userAddress</code>, those fields are private which means that you have to
have public getters and setters which match the form
<code>getCompanyName</code> and <code>setUserAddress</code>, but when you refer
to those bean properties, you leave off the <code>get</code> and
<code>set</code> and follow the capitalization of the field names
(<code>companyName</code>)&mdash;the private names, even though you're using
the public accessors with different capitalization.</p>

<p>Compound this with the fact that <em>the right way</em> is to use Spring, which instantiates these objects lazily and introspects their properties and... well, you get to find your capitalization typos at runtime.</p>

<p>Whereas in a language or framework or toolkit which allowed you to define
<em>syntax</em> for all of these things would let you validate even
capitalization conventions (hey, if such a parser existed, it could have
opinions about things like this!) and let you know far, far before some hapless
tester clicking his way through a 38 page test script hit a runtime error that
you made a silly typo...</p>

<p>... because <a href="http://www.modernperlbooks.com/mt/2010/07/dont-parse-that-string.html">hoping that unstructured data is correct without ensuring that it is correct is a recipe for errors</a>.</p>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/04/08/the-verifiability-of-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beans versus Immutable Objects</title>
		<link>http://www.modernperlbooks.com/mt/2011/04/beans-versus-immutable-objects.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/04/beans-versus-immutable-objects.html#comments</comments>
		<pubDate>Tue, 05 Apr 2011 16:31:00 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perl6]]></category>
		<category><![CDATA[typing]]></category>
		<category><![CDATA[webprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Moose revealed itself as Good Code to me when I realized that it encourages Good Design. If you follow the example code and use the API it exposes in a natural way, you tend to write well-organized and reasonably safe...]]></description>
			<content:encoded><![CDATA[
        <p><a href="http://moose.perl.org/">Moose</a> revealed itself as Good Code to me when I realized that it encourages Good Design. If you follow the example code and use the API it exposes in a natural way, you tend to write well-organized and reasonably safe code (where "safe" means "you paint yourself into fewer corners").</p>

<p>That's the sign of a very good library or framework: the common things are
easy and they resulting code is easy to understand, maintain, and extend.</p>

<p>Moose has a very nice feature of reducing the surface area of the APIs you create with it. You can declare a class with several attributes but only expose a few public getters or setters. The ability to use lazy initialization of attributes&mdash;as well as default values&mdash;allows the calculation of dependent attributes as needed.</p>

<p>Best yet, if you can arrange your classes in such a way as to provide their
necessary attribute values when you call their constructors, you can treat your
objects from the outside as immutable, and you're halfway toward inversion of
control, if that's useful to you.</p>

<p>(Read more about Moose and immutability and the value of these features and
designs in the <a
href="http://onyxneon.com/books/modern_perl/index.html">Modern Perl book</a>,
available in several freely redistributable electronic forms.)</p>

<p>Think of all of this as not coloring outside the lines. While good art is
aware of the structure and limitations of its chosen form and breaks those
conventions when it makes for better art, good software is primarily a
conversation mechanism where perhaps you don't want to try to outdo Pynchon or
DeLillo or Wallace. Perhaps.</p>

<p>Contrast this mechanism of working with objects with the bean style as
expressed in parts of the Java world: a class has several private attributes.
Each of those attributes has public getters and setters which follow a standard
naming convention. Objects interact with other objects by their conformance to
a protocol and discovery and use of those getters and setters enabled by the
magic genericity of reflection.</p>

<p>(<a href="http://c2.com/cgi/wiki?DuckTyping">Did anyone else hear a duck
quack</a>?)</p>

<p>For the convenience of allowing a dependency injection such as Spring or a
web framework inspired by J2EE to manage some of the boilerplate of managing
object instantiation or object lifespan or even mapping object data to and from
strings for web or web request display, you not only give up the
<em>creation</em> of your objects, but you expose their internal attributes
behind a thin (and, be honest, autogenerated by your IDE when it pops up a
warning!) layer of accessor method.</p>

<p>There <em>is</em> value in the loose and ad hoc adherence to a protocol of
practice...</p>

<p>... but for a language which claims great maintainability benefits from
forcing the use of static, manifest typing, you may encounter a fair few
perplexing errors if you don't get the CamelCase type of your bean property
just so. (Remember, reflection!)</p>

<p>While sometimes I do wish Perl 5 were stricter about types and signatures&mdash;certainly I look forward to using Perl 6's gradual typing for more projects&mdash;I have more sense of safety with the Perl approach, where we don't assume our language is completely safe by itself. We're a little more paranoid, and&mdash;at least in this case&mdash;I believe it leads to better designs.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/04/05/beans-versus-immutable-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Perl can Avoid Java&#8217;s Worst Web Messes</title>
		<link>http://www.modernperlbooks.com/mt/2011/04/how-perl-can-avoid-javas-worst-web-messes.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/04/how-perl-can-avoid-javas-worst-web-messes.html#comments</comments>
		<pubDate>Fri, 01 Apr 2011 17:18:07 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[oo]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[webprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I've spent the past month developing an Enterprise Class web site (which means &#34;a CRUD application written mostly in XML configuration files with a smattering of Java here and there&#34;) to help out a colleague who shall remain nameless. The...]]></description>
			<content:encoded><![CDATA[
        <p>I've spent the past month developing an Enterprise Class web site (which means "a CRUD application written mostly in XML configuration files with a smattering of Java here and there") to help out a colleague who shall remain nameless.</p>

<p>The good news is that the Perl world (at least for those enlightened souls fortunate enough to be able to use modern Perl techniques) has little to worry about from the Enterprise Class world.</p>

<p>The bad news is that some of the problems afflicting Perl&mdash;especially for people who don't write it all day, every day&mdash;are common to software development in general.</p>

<p>For example: this CRUD project uses an Apache framework which has been
around for most of a decade and is on its second iteration. Apache projects get
a lot of credit for rigorous documentation...</p>

<p>... except that their documentation in this case is, as with most open
source projects, a mish-mash of conceptual overviews, API references,
contributed guides, tutorials, and links to mailing list archives and blog
postings, with little narrative flow between them and, worse, often precious
little internal information on which version of the framework applies.</p>

<p>Search for information with your favorite search engine and be ready to wade
through inapplicable and inappropriate data, whether wrong from the start or
rendered irrelevant several years later. (This is as much an indictment of
search engines as anything else.)</p>

<p>Perhaps the most concerning trend in this project is that the Enterprise Class nature of the project betrays a desire <em>not</em> to express any design opinion. In fact, the dominant design opinion seems to be "Anything is up for grabs"&mdash;you can perform validation in several different ways, you have your choice of several competing templating engines, you can map URIs to controller actions in at least three ways, and there are at least two (possibly more) mechanisms of accessing model data in templates.</p>

<p>The cost of all of this complexity exposes itself in two places. First, you have to decide (through coding standards or stumbling into a design you like) <em>how</em> to structure your application before you can get anything done. Second, you swim through a Styxian deluge of XML to configure (and reconfigure and modify and adapt and configure and reconfigure...) anything and everything.</p>

<p>(A plugin exists which nominally supports convention over configuration, but
the lack of documentation and, apparently, interest renders it far too
risky.)</p>

<p>I hesitate to blame too much the authors of the framework for this mess (and, as such, refuse to name the framework).
Certainly some of their design choices reflect fundamental flaws of the Java
language itself (in particular, Java's relative inflexibility especially with
regard to its silly static manifest type system combined with the IDE-based
tooling necessary to manage its verbosity and lack of abstraction make even
programming in XML seem more flexible). Even so, the ceremony necessary to
produce even the single CRUD steps of "Create an item and view it" involves
editing multiple XML files, creating several Java classes, and&mdash;if you do
things the Enterprisey way&mdash;creating and configuring subtemplates for the
entire layout of the site as well as embracing the inversion of control object
instantiation and initialization pattern (and given the unpopularity of the
immutable object pattern in Java as compared to bean-style mutators, it's not
easy to reason about the state of your objects) is damning.</p>

<p>The lessons for Perl are simple:</p>

<ul>

<li>Avoid the overcomplexity of projects by emphasizing the flexibility and <em>simplicity</em> of the language. Yes, you read that right. Meditate on it.</li>

<li>Write better documentation.</li>

<li>When the best way to do something changes, deprecate old documentation and point to the new approach.</li>

<li>Emphasize simplicity. Make the default a good way to do most things.
Provide comprehensive tutorials which explain the entire system using that
effective default.</li>

<li>Provide smaller upgrade paths and avoid big-bang incompatible releases
which fork documentation and knowledge.</li>

<li>Unify where possible.</li>

</ul>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/04/01/how-perl-can-avoid-javas-worst-web-messes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Perl Could Learn from Java WARs</title>
		<link>http://www.modernperlbooks.com/mt/2011/03/what-perl-could-learn-from-java-wars.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/03/what-perl-could-learn-from-java-wars.html#comments</comments>
		<pubDate>Wed, 23 Mar 2011 17:38:15 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[webprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[A business partner convinced me to work on a short-term Java web programming contract with him, and the details were right. (My most recent Java programming experience was an experimental project for a very large middleware company a couple of...]]></description>
			<content:encoded><![CDATA[
        <p>A business partner convinced me to work on a short-term Java web programming contract with him, and the details were right. (My most recent Java programming experience was an experimental project for a very large middleware company a couple of years ago that gave them a lot of information at the cost of a lot of frustration.)</p>

<p>The experience has been enlightening.</p>

<p>One bright spot is Java's notion of web deployment. It has its obvious flaws, but it has obvious benefits. You gather all of the artifacts necessary for your project: XML files, JAR files (bundles of Java libraries), web templates, the classes you've written, and other resources, all in the appropriate subdirectories governed partly by convention and partly by a manifest file. Your deployment process bundles everything up in to a WAR file (a web archive). Then you dump this WAR file into an application server.</p>

<p>Compare that to another project I delivered recently where I sent a
CPAN-style distribution tarball to a (much more technically savvy) client, who
set about installing dependencies from the CPAN (where <a
href="http://search.cpan.org/perldoc?Module::Install">Module::Install</a>
couldn't get the job done) and from <code>yum</code>, where you don't want CPAN
installing, for example, <a href="http://postgresql.org">PostgreSQL</a> for
you.</p>

<p>Granted, I could have used a tool such as <a href="http://search.cpan.org/perldoc?Shipwright">Shipwright</a> to bundle my code, but I didn't. For better or worse, CPAN-style distribution is awfully common.</p>

<p>Imagine instead if I <em>could</em> have bundled everything up into a single WAR-style file which a hypothetical Perl web application server could deploy.</p>

<p>Java achieves this in at least two ways. First, Java wants to own the world&mdash;Java does have bindings to native code, but the Java desire to have pure-Java everywhere tends to encourage people to write pure-Java code where Perl hackers might write XS. (XS means you have to compile native code on the target deployment platform.) <a href="http://www.modernperlbooks.com/mt/2010/09/less-xsmore-ctypes.html">Someday perhaps Perl 5 can reduce its reliance on XS</a>.</p>

<p>Second, Java application servers also have the advantage of a stronger memory separation between different applications running on a single JVM. Some of this is due to the static nature of the Java language. When you can resolve all references within an application at load time, you don't have to look things up by strings in a global directory. (Of course, you do have to be clever in your VM design to allow runtime reflection, so the implementation of the VM is also important.)</p>

<p><a href="http://plackperl.org/">Plack</a> offers a way around this. Imagine a Perl app server based on Plack to which you can deploy multiple applications. The app server could start multiple independent backends&mdash;even so far as having multiple installations or at least library directories for each application, as necessary&mdash;to use Unix process separation to achieve this memory separation.</p>

<p>Such an approach would still likely use less memory than Java.</p>

<p>Make the Plack app server a reverse proxy capable of serving static files
and SSL and redirecting invalid requests and you might have something really
exciting. (Make it cloud aware and give me a 10% equity stake.)</p>

<p>These are idle musings, and the Java ghetto doesn't tempt me to ditch the
world of Perl for a work life full of beans and mappers and inversion of
control governed by
there's-no-angle-bracket-shortage-in-this-sea-of-XML&mdash;but even in that
mess, there are still some good ideas worth borrowing, buffing, and
bettering.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/03/23/what-perl-could-learn-from-java-wars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>java2perl6api – Java to Perl 6 API translation – What, Why, and Whereto</title>
		<link>http://blog.timbunce.org/2010/07/16/java2perl6api-java-to-perl-6-api-tranalation-what-why-and-whereto/</link>
		<comments>http://blog.timbunce.org/2010/07/16/java2perl6api-java-to-perl-6-api-tranalation-what-why-and-whereto/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 17:12:59 +0000</pubDate>
		<dc:creator>TimBunce</dc:creator>
				<category><![CDATA[dbdi]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl6]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://timbunce.wordpress.com/?p=460</guid>
		<description><![CDATA[In this post I&#8217;m going to talk about the java2perl6api project. What its goals are, why I think it&#8217;s important, how it relates to a Perl 6 DBI, what exists now, what&#8217;s needs doing, and how you can help. Firstly I&#8217;d like to point out that, funnily enough, I&#8217;m not very familiar with Java or [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&#38;blog=2562816&#38;post=460&#38;subd=timbunce&#38;ref=&#38;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;m going to talk about the java2perl6api project. What its goals are, why I think it&#8217;s important, how it relates to a Perl 6 DBI, what exists now, what&#8217;s needs doing, and how you can help.<br />
<span id="more-460"></span></p>
<p>Firstly I&#8217;d like to point out that, funnily enough, I&#8217;m not very familiar with Java or Perl6. It&#8217;s entirely possible that I&#8217;ll make all sorts of errors in the following details. If you spot any do please let me know.</p>
<h2>Background</h2>
<p>The Java language ecosystem is big and mature after years of heavy investment of time and money.</p>
<p>It doesn&#8217;t have a central repository of Open Source modules like CPAN (though <a href="http://en.wikipedia.org/wiki/Apache_Maven">Maven</a> repositories <a href="http://download.java.net/maven/1/">like</a> <a href="http://repo1.maven.org/maven2/">these</a> are similar I guess). It does, however, have a number of mature high quality class libraries, and a very large number of developers familiar with those libraries (more on that below).</p>
<h2>Goals</h2>
<p>The primary goal of the java2perl6api project is to make it easy to create Perl 6 class libraries that <em>mirror</em> Java equivalents. By <em>mirror</em> I mean share the same method names and semantics at a high level (though not at a low-level, more on that below).</p>
<p>Secondary goals are to do that well enough that:</p>
<ul>
<li>the documentation for Java classes can serve as primary the documentation for the corresponding Perl 6 classes. The Perl 6 classes need only document the differences in behavior, which these should be minimal and &#8216;natural&#8217;. The same applies to books describing the Java classes.
</li>
<li>Java developers familiar with the Java classes should feel comfortable working with the corresponding Perl 6 classes.
</li>
<li>and, hopefully, some way can be found to convert test suites for the Java classes into Perl 6 code that&#8217;ll test the corresponding Perl 6 classes. (I appreciate that this is a non-trivial proposition, but there are viable approaches available, like <a href="http://www.xmlvm.org/overview/">xmlvm</a>.) Even if that can&#8217;t be done, extracting and translating tests manually is less work, and more effective, than creating them from scratch for a new API.
</li>
</ul>
<h2>Why?</h2>
<p>Firstly, creating good APIs is hard. Java APIs like <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/technotes/guides/jdbc/">JDBC 3.0</a> and <a href="http://java.sun.com/developer/technicalArticles/javase/nio/">NIO.2</a> are the result of years of professional effort and demanding commercial experience. Why not build on that experience?</p>
<p>I appreciate that Java APIs are often limited by the constraints of the language, such as the lack of closures, and that Perl 6 can probably express any given set of semantics more effectively than Java. My point here is that some Java APIs embody, however inelegantly, years of hard won experience that we can benefit from. I&#8217;d rather make new mistakes than repeat old ones.</p>
<p>Secondly, there are many more Java developers than Perl developers. Many <em>many</em> more if job vacancies are any indication:</p>
<p><img src="http://www.indeed.com/trendgraph/jobgraph.png?q=%22perl+developer%22,%22java+developer%22" alt="job vacancy trends for perl developer and java developer" height="300" width="540" /></p>
<p>I think we&#8217;d be foolish not to try to smooth the path for any Java developers who might be interested in Perl 6. The java2perl6api project is just one small aspect of that.</p>
<p>I really hope someone starts writing a &#8220;Perl 6 for Java Developers&#8221; tutorial. Perl 6 has the potential to become a very popular language<sup><a href="http://blog.timbunce.org/2010/07/16/java2perl6api-java-to-perl-6-api-tranalation-what-why-and-whereto/#1">1</a></sup>. Getting just a tiny percentage of Java developers (and Computer Science majors and their teachers) interested in it could be a big help.</p>
<p>Thirdly, any future DBI for Perl 6 and Parrot needs a much better foundation than the very limited and poorly defined one that <a href="http://search.cpan.org/~timb/DBI-1.611/lib/DBI/DBD.pm">underlies the Perl 5 DBI</a>. I plan to adopt the JDBC 3.0 API <em>and test suite</em> for that <em>internal</em> role. (You could call this a &#8220;Test Suite Driven Strategy&#8221;.) I&#8217;ll talk more about that in a future blog post.</p>
<h2>The History java2perl6api</h2>
<p>I&#8217;ve been kicking around various ideas for integrating Java and Perl6/Parrot for years. I think I first decided to use JDBC as the inspiration for the DBI-to-driver API in 2006.</p>
<p>You may remember back in 2004, around the 10th anniversary of the DBI, the <a href="http://www.perlfoundation.org/">Perl Foundation</a> setup a &#8220;DBI Development Fund&#8221; that people could <a href="http://dbi.perl.org/donate/">donate</a> to. I&#8217;ve never drawn any money from that fund. I want to use it to oil other peoples wheels.</p>
<p>In 2007 <a href="http://news.perlfoundation.org/2007/03/best-practical-sponsors-perl-6.html">Best Practical sponsored Perl 6 Microgrants</a> through the Perl Foundation. I asked if I could piggyback my idea for a Java to Perl 6 API translator onto their microgrant management process but using money from the DBI Development Fund. TPF and Best Practical kindly agreed. I posted a description of the task and Phil Crow volunteered and was <a href="http://news.perlfoundation.org/2007/04/phil-crow-to-create-jdbc-api-f.html">awarded the microgrant</a> in April 2007.</p>
<p>At OSCON in July 2007 I gave lightning talk called &#8220;<a href="http://www.slideshare.net/Tim.Bunce/dbi-for-parrot-and-perl-6-lightning-talk-2007">Database interfaces for open source languages suck</a>&#8221; which explained the rationale for using JDBC as a foundation for the DBI-to-driver API and mentioned Phil&#8217;s java2perl6 project.</p>
<p>Development ground to a halt around the end of 2007 for various reasons. It picked up again for a few months after OSCON 2009 (where I gave a short lightning talk asking for help) then stalled again in October. Partly because we seemed to have hit a limitation with Rakudo and partly because I was focussed on Devel::NYTProf <a href="http://blog.timbunce.org/2009/12/24/nytprof-v3-worth-the-wait/">version 3</a> and then <a href="http://blog.timbunce.org/2010/06/09/nytprof-v4-now-with-string-eval-x-ray-vision/">version 4</a>, which took <em>way</em> more time than I expected.</p>
<p>There&#8217;s life in the project again now. We&#8217;ve dodged the earlier problem, put the <a href="http://github.com/timbunce/java2perl6">code on github</a>, brought it into sync with current <a href="http://rakudo.org/">Rakudo</a> Perl 6 syntax, and generally instilled some momentum.</p>
<h2>The Current java2perl6api</h2>
<p>Let&#8217;s take a look at a simple example.</p>
<p>To generate a perl6 file that mirrors the API of the java.sql.Savepoint class you&#8217;d just execute java2perl6api like this:</p>
<pre style="background-color:#ddd;margin:2em;padding:1em;">$ java2perl6api java.sql.Savepoint
loading java.sql.Savepoint
wrote java/sql/Savepoint.pm6 - interface java.sql.Savepoint
checking java/sql/Savepoint.pm6 - interface java.sql.Savepoint
</pre>
<p>That&#8217;s loaded and parsed the description of the java.sql.Savepoint class (from the <a href="http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/javap.html">javap</a> command), generated a corresponding perl6 module, and run perl6 to validate it.</p>
<p>The generated module (with some whitespace and cruft removed) looks like this:</p>
<pre style="background-color:#ddd;margin:1em;padding:1em;">use v6;
role java::sql::Savepoint {
    method getSavepointId (
    --&gt; Int   #  int
    ) { ... }
    method getSavepointName (
    --&gt; Str   #  java.lang.String
    ) { ... }
};
=begin pod
=head1 Java
  Compiled from "Savepoint.java"
  public interface java.sql.Savepoint{
      public abstract int getSavepointId() throws java.sql.SQLException;
      public abstract java.lang.String getSavepointName() throws java.sql.SQLException;
  }
=end pod
</pre>
<p>The pod section shows the description of the class that javap returned. The java2perl6api utility parsed that <a href="http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/concepts/interface.html">Java interface</a> and generated the corresponding <a href="http://perlcabal.org/syn/S14.html#Roles">Perl6 role</a>. The &#8216;java.sql.Savepoint&#8217; has been mapped to &#8216;java::sql::Savepoint&#8217;. The generated methods are stubs using <code>...</code> (the &#8220;yada, yada, yada&#8221; operator). The types int and java.lang.String have been mapped to Int and Str. Because the only types used were built-ins, no type declarations were added.</p>
<p>Currently java2perl6api handles the above plus overloaded methods (which generate <a href="http://perlcabal.org/syn/S12.html#Multisubs_and_Multimethods">multi methods</a>), multiple implements clauses (which generate multiple <a href="http://perlcabal.org/syn/S14.html#Compile-time_Composition">does</a> clauses). There&#8217;s also partial support for class/interface constants (which currently generate exported methods).</p>
<p>The default behavior is to recursively process any Java types referenced by the class which aren&#8217;t mapped to Perl 6 types. So executing <code>java2perl6api java.sql.Connection</code>, for example, will generate 48 Perl 6 modules! (Because <code>java.sql.Connection</code> refers to many types, including <code>java.sql.Array</code> which refers to many types including <code>java.sql.ResultSet</code> which refers to <code>java.net.URL</code> which refers to <code>java.net.Proxy</code> etc. etc.) The <code>--norecurse</code> options disables this behavior.</p>
<p>Normally you&#8217;ll want to use the recursion but instead of letting it drill <em>all</em> the way into the Java types, you would supply your own &#8216;typemap&#8217; specification via an option. That tells java2perl6api which Java types you want to map to which Perl 6 types. So instead of recursing into the <code>java.net.URL</code> type to generate a <code>java/net/URL.pm6</code> file, for example, you can tell java2perl6api to use a specific Perl 6 type. Perhaps just <code>Str</code> for now.</p>
<h2>How this relates to JDBC / DBDI / DBI v2</h2>
<p>I want to start applying java2perl6api to the <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/technotes/guides/jdbc">JDBC</a> classes now to create a &#8220;Database Driver Interface&#8221; or &#8220;DBDI&#8221; for Perl 6.</p>
<p>Starting with the <a href="http://download-llnw.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/sql/DriverManager.html">DriverManager</a> class and the <a href="http://download-llnw.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/sql/Connection.html">Connection</a>  interface I&#8217;ll use java2perl6api to generate corresponding Perl 6 roles with <em>heavy</em> stubbing out of types. Basically anything I don&#8217;t need to think about right now will be mapped to the <code>Any</code> type.</p>
<p>I&#8217;ll start fleshing out some basic implementation logic for each in a Perl 6 class that <a href="http://perlcabal.org/syn/S14.html#Compile-time_Composition">does</a> the corresponding role. I&#8217;ll probably use PostgreSQL as the first driver and the guts of <a href="http://github.com/mberends/MiniDBI/blob/master/lib/MiniDBD/Pg.pm6">MiniDBD::Pg</a> as inspiration.</p>
<p>The first minor milestones will be creating connections, then execute non-selects, then selects then prepared statements. Somewhere along the way I expect they&#8217;ll be a Perl 6 DBDI driver implemented for the <a href="http://blogs.perl.org/users/martin_berends/2010/06/rakudo-perl-6-gets-into-databases.html">Perl 6 MiniDBI project</a>. The next key step would be to start refactoring the code heavily so anyone wanting to implement a new driver should only have to implement the driver specific parts. (There are some JDBC driver toolkits that can provide useful ideas for that.)</p>
<h2>What needs doing</h2>
<p>There&#8217;s a <a href="http://github.com/timbunce/java2perl6/blob/master/TODO">TODO file in the repository</a> that lists the current items that need working on.</p>
<p>One fairly simple item is to add a <code>--prefix</code> option to specify an extra leading name for the generated role. So <code>java.sql.Savepoint</code> with a prefix of <code>DBDI</code> would generate a <code>DBDI::java::sql::Savepoint</code> role.</p>
<p>Another item, less simple but more important, is to automatically discover the values of constants and embed them into the generated file. Probably the best way to do that is to extend <a href="http://github.com/timbunce/java2perl6/blob/master/lib/Java/Javap/javap.grammar">the parser</a> (which uses <a href="http://search.cpan.org/perldoc?Parse::RecDescent">Parse::RecDescent</a>) to parse the verbose-mode output of javap, which includes those details.</p>
<p>There are <a href="http://github.com/timbunce/java2perl6/blob/master/TODO">plenty of others</a>.</p>
<h2>How you can get involved</h2>
<p>Firstly, come and say &#8220;Hi!&#8221; in the <a href="irc://chat.freenode.net/#dbdi">#dbdi</a> IRC channel on irc.freenode.net.</p>
<p>The code is on <a href="http://github.com/timbunce/java2perl6">github</a>. You can get commit access by asking on the <a href="irc://chat.freenode.net/#perl6">#perl6</a> channel.</p>
<p>There&#8217;s also a mailing list at <a href="mailto:dbdi-dev@perl.org">dbdi-dev@perl.org</a> which you can <a href="mailto:dbdi-dev-subscribe@perl.org">subscribe</a> to.</p>
<p>I look forward to hearing from you!</p>
<hr />
<ol>
<li><a name="1"></a><br />
When I say &#8220;Perl 6 has the potential to become a very popular language&#8221; I do so with typical British <a href="http://en.wikipedia.org/wiki/Understatement">Understatement</a>.
</li>
</ol>
<br />Filed under: <a href='http://blog.timbunce.org/category/tech/software/perl/'>perl</a>, <a href='http://blog.timbunce.org/category/tech/software/'>software</a> Tagged: <a href='http://blog.timbunce.org/tag/dbdi/'>dbdi</a>, <a href='http://blog.timbunce.org/tag/java/'>java</a>, <a href='http://blog.timbunce.org/tag/perl/'>perl</a>, <a href='http://blog.timbunce.org/tag/perl6/'>perl6</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timbunce.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timbunce.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timbunce.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timbunce.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timbunce.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timbunce.wordpress.com/460/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timbunce.wordpress.com/460/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timbunce.wordpress.com/460/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&amp;blog=2562816&amp;post=460&amp;subd=timbunce&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.timbunce.org/2010/07/16/java2perl6api-java-to-perl-6-api-tranalation-what-why-and-whereto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://1.gravatar.com/avatar/1cf82705f5ab43c73273ab5d690866b3?s=96&amp;amp;d=identicon&amp;amp;r=G" length="" type="" />
<enclosure url="http://www.indeed.com/trendgraph/jobgraph.png?q=&quot;perl developer&quot;,&quot;java developer&quot;" length="" type="" />
<enclosure url="http://www.indeed.com/trendgraph/jobgraph.png?q=&quot;perl developer&quot;,&quot;java developer&quot;" length="" type="" />
		</item>
	</channel>
</rss>

