## Java, and Cloning Arrays

Just to let you know, in case you find this kind of problem… Java is able to clone uni-dimensional arrays of built-in types, like int, but it is not able to clone multidimensional arrays, as a multidimensional array is an array of arrays, and an array is an object. Not sure this is the explanation, but the truth is… clone is useless in this evil language.

If you want to prove it yourself, just try the following code.

public class TestClone {

public static void main(String[] args) {

int[][] boardM = new int[1][1];
int[][] copyM;

boardM[0][0] = 1;
copyM = boardM.clone();

boardM[0][0] = 2;

if (copyM[0][0] == boardM[0][0]) {
System.err.println("oops?");
}

int[] boardV = new int[1];
int[] copyV;

boardV[0] = 1;
copyV = boardV.clone();

boardV[0] = 2;

if (copyV[0] == boardV[0]) {
System.err.println("oops, too?");
}
}
}


## Javascript Reversi

In the last two days I engaged in developing Reversi, just to learn how the minimax algorithm works. To make it easier to share, and remove GUI toolkit dependencies, my approach was using HTML (a simple page with a 8×8 table), three images (empty cell, black or white cells), a CSS file that fills empty cells or used cells, and highlights movement possibilities, and a couple of JavaScript files (jquery, a reversi-board.js file to handle the board as an object, a reversi.js file to handle the interface between the board and the HTML file, and finally a minimax.js file to handle the minimax algorithm.

At the moment the game is playable, and not too slow. The code can be optimized to make it faster. In the next couple of days I might do that.

Also, regarding the AI code, it can be made better. In one side, the minimax algorithm can try to analyze more moves in advance (only three at the moment), in the other, the board evaluation function can be made better as well.

If you wish to play, go ahead: http://eremita.di.uminho.pt/~ambs/reversi

## Dotcloud and Perl Versions

Today I waste all my afternoon trying to solve a problem with Perl versions and DotCloud. I say I wasted the afternoon because I could not solve the issue during the afternoon, only in the beginning of the night.

Enough complain, and let me explain what was going on. Yes, this post is a little as a complain to the DotCloud tech guys, but also as a guide to someone that stumble in a similar situation.

I had a service running with Dancer. It worked, it connected perfectly to the database. Next step was to set up a cron job to fetch some data from the Web. I added its dependencies in the Makefile.PL file for the website, and hoped that the deploy system would install them. But the cron job continued failing. I tried to force its installation, but the deploy system said it was up to date. Something wrong was going on. I forced a little more the installation, adding the URL to the module tarball. This way the deploy system could not guess its version, and therefore, would install the module anyway. It worked, but the deploy system continued complaining about other and other module.

It got stranger when the cron job complained about the lack of the DBI module. Hey, how can it be, if the website is running and using it? Well, DBI is based on C code, so probably I am missing to include a sub-folder of the local module installation. And then, it got clearer: the cron job complained that the version of DBI.so could not be loaded into Perl because of unresolved symbols. Aha! Then, there are two Perl versions.

And indeed, the web application was running Perl 5.14.x (I requested it on my DotCloud configuration file) and the Perl used by the cron job was Perl 5.10.x. Basically, DotCloud folks keep the system Perl, and install custom Perl versions under /opt/perl5, and creates some symlinks there, so that /opt/perl5/perls/current points to the Perl version requested.

So, the solution is to make the cron job use the correct Perl version. In my case I set the full path, but you could change the PATH environment variable in your .bashrc-like file.

This wasn’t that hard, but it was quite time consuming, because deploys take some time. Probably I could get faster if I remembered to test Perl versions right ahead, but I forgot to.

I suggest DotCloud guys to do one of two things: or document this in the same doc where it is explained how to request a specific Perl version, or fix the damn environment so that cron jobs and other code uses the same Perl version as the one requested by the system.

## Java User-Friendness

I can’t see how people say Java is an end-user language, whose applications are for general user usage…

SEVERE: Exception
javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: "fo:inline" is not a valid child of "fo:block"!  (See position 1870:716)
at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:302)
at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130)
at org.apache.fop.cli.Main.startFOP(Main.java:174)
at org.apache.fop.cli.Main.main(Main.java:205)
Caused by: javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: "fo:inline" is not a valid child of "fo:block"!  (See position 1870:716)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:501)
at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)
... 3 more
Caused by: org.apache.fop.fo.ValidationException: "fo:inline" is not a valid child of "fo:block"!  (See position 1870:716)
at org.apache.fop.events.ValidationExceptionFactory.createException(ValidationExceptionFactory.java:38)
at org.apache.fop.events.EventExceptionManager.throwException(EventExceptionManager.java:54)
at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBroadcaster.java:175) at$Proxy1.invalidChild(Unknown Source)
at org.apache.fop.fo.FONode.invalidChildError(FONode.java:534)
at org.apache.fop.fo.flow.Inline.validateChildNode(Inline.java:123)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:267) at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:171) at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1072) at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source) at org.apache.xerces.xinclude.XIncludeHandler.startElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484) ... 4 more --------- javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: "fo:inline" is not a valid child of "fo:block"! (See position 1870:716) at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:501) at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299) at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130) at org.apache.fop.cli.Main.startFOP(Main.java:174) at org.apache.fop.cli.Main.main(Main.java:205) Caused by: org.apache.fop.fo.ValidationException: "fo:inline" is not a valid child of "fo:block"! (See position 1870:716) at org.apache.fop.events.ValidationExceptionFactory.createException(ValidationExceptionFactory.java:38) at org.apache.fop.events.EventExceptionManager.throwException(EventExceptionManager.java:54) at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBroadcaster.java:175)
at $Proxy1.invalidChild(Unknown Source) at org.apache.fop.fo.FONode.invalidChildError(FONode.java:534) at org.apache.fop.fo.flow.Inline.validateChildNode(Inline.java:123) at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:267)
at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:171)
at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1072)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484)
... 4 more
---------
org.apache.fop.fo.ValidationException: "fo:inline" is not a valid child of "fo:block"!  (See position 1870:716)
at org.apache.fop.events.ValidationExceptionFactory.createException(ValidationExceptionFactory.java:38)
at org.apache.fop.events.EventExceptionManager.throwException(EventExceptionManager.java:54)
at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBroadcaster.java:175) at$Proxy1.invalidChild(Unknown Source)
at org.apache.fop.fo.FONode.invalidChildError(FONode.java:534)
at org.apache.fop.fo.flow.Inline.validateChildNode(Inline.java:123)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:267) at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:171) at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1072) at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source) at org.apache.xerces.xinclude.XIncludeHandler.startElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484) at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299) at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130) at org.apache.fop.cli.Main.startFOP(Main.java:174) at org.apache.fop.cli.Main.main(Main.java:205)  Just great. Now I know exactly what to do. Cry! ## Nine tools I can’t live without (for Windows) First, a relevant disclaimer: I use Windows mostly for two purposes, test my Perl modules under a Windows environment, and printing some stuff as my Mac sometimes can’t find some Windows printers in the network. This said, my Windows runs in a virtual machine, and I install these tools right after installing Windows. ## Browser The first thing you need to know is that Windows does not include a web browser. It includes a tool that is mainly used to download a Web browser. Its name is Internet Explorer, and you can use it to download Firefox, Google Chrome, Safari or Opera. In my main machine, I use Firefox. There are some features that I like that are not available on other browsers. But for my virtual machine I need a tool to check my mail, and to download some other tools. Not much more. ## Antivirus Now I am cheating. In my virtual machine I do not have an antivirus installed. It is too easy to reinstall a new virtual machine, and you all know that an antivirus running is wasting some resources that can be relevant for virtual machines. Nevertheless, I want to suggest two antivirus. In fact, I suggest these two because they are the two I install when I need one (note that I do not install both, I install one or other). I am not sure what is the best. Avira seems to be lighter, AVG seems to be more professional and heavy. Both these antivirus software have their professional paid version. This means that if you like their performance, you can buy the paid version for better protection. They also include some functionality that are common on current antivirus software, like trojan detection and web site security. ## Connectivity I need to transfer files to and from my Mac and/or Linux servers to the Windows machine. For that I can make the file available to download, or I can use a tool to copy files. As FTP is not that secure, the best approach is to use SCP or Secure Copy. For that, I suggest WinSCP tool. It also support FTP, but its main strength is the ability to use the SSH protocol. A similar necessity raises when I need to connect to a Linux server from Windows. Again, I do not need this when using a virtual machine, as I can use a terminal from the host machine to ssh anywhere I want. But if you have a dual boot machine, or you just work on windows, then you will need this. Putty is the better telnet/ssh client I ever seen for Windows. It not only supports ssh, but it also supports ssh keys, making your connection even more secure. ## Version Control My main repository of code is still a SubVersion one. I am now using GitHub a lot, too, but my main tools are still in the old SVN repository. To checkout and commit from Windows I use TortoiseSVN. Not only because it has a nice and cute logo, but because it integrates perfectly in Explorer. Also, I love a recent feature, where TortoiseSVN commit message window auto-completes your commit messages with the filenames or contents of the modified files. Very cool. ## Text Editor I do not use that much Windows to have a dedicated IDE. I usually use Notepad++ for my editing needs. It knows about file encoding, it knows about end-of-line differences among operating systems, it knows how to highlight main programming languages, it is light and easy to use. It is not full of features, but I do not need them. Or at least, I think I don’t. Also, I confess I never dig much all of its potentialities. ## Programming Language My main programming language is Perl. I occasionally need some C, and some of my Perl modules link to C and C++. So, to fill my need for these three programming languages I choose to install Strawberry Perl. I never ever used Perl with so ease in Windows before Strawberry Perl. It supports more than 90% of CPAN modules, and it includes a C and C++ compilers (gcc based) and a make command. What else one needs? ## Compression Tool Windows knows (it its recent incarnations) about what are zip files. But it ignores all about rar, gzip, bzip2 or tar files. Most Perl modules come in tgz format, so I need a tool to extract them. Most people tend to use WinRar or 7zip. I am not sure why, but I prefer IzArc. It is lightweight, supports most compression algorithms, and integrate pretty well with explorer. ## PDF Visualizer If up to now I defended the use of lightweight applications, now I’m going to the other side, and my choice to read PDF documents is Adobe Reader. There are other free and more lightweight applications, like FoxIt Reader, but I had so many trouble with PDFs and PDFs compatibility that I prefer to have the application from the PDF creators. I know that doesn’t mean much, but at least it can work as an excuse. ## What I miss… There are some tools that I miss. First, I miss a decent command prompt window. One that let me select text, copy and paste easily, that let me make full screen, that let me change easily the font, and not just some stupid predefined sizes. Together with this, a decent command prompt, or shell, with better completion, better prompt configuration, better history, etc. To install cygwin is not an option. For that, I would use linux. Also, and as I stated above, now I am using GitHub a lot. Then, I miss a Git client. I tried TortoiseGit but it didn’t work well for me. Probably it was a beta release, and now it is better. I need to test. I used the Git binary, that includes a Git shell, and mostly works. But it do not integrate well with the operating system. ## Artificial Intelligence for Games errata This year I am teaching Artificial Intelligence for Games. I grabbed some book PDFs from the Internet (yes, sorry, it would be impossible to buy all the books to choose one, and libraries around doesn’t have books in this thematic) and I chose a pair of books to buy. One of them is this Artificial Intelligence for Games, by Ian Millington and John Funge. The book is quite complete, the language very accessible, full of code, lots of insights about that developing AI for games means. I really like the book, and I will continue to use in future years if they let me continue teaching this course. Unfortunately the book is full of small typos and problems. There is an official errata available but it is very incomplete. I started writing my own errata (I’m not repeating the items in the official errata, just adding new errors I find) with some fixes, and some insights on some interpretation problems. I warned Ian Millington about my errata, and he will, hopefully, go through my errata items and decide if they should be included in the official errata. While Ian doesn’t have the time for that, I am sharing my own errata page. Note that I am not professional on this area and I might have misunderstood something… ## @GitHub Inconsistency This is something I couldn’t still understand. Not sure if this is a bug or if I am missing some information. This project claims to have 22 open issues, but when I get into the issues page, it shows 24 issues (and the list really includes 24 items). I would say the number in the toolbar is wrong, but probably I am missing some special type of issue that does not get counted? ## Being a Dancer (re-post from blogs.perl.org) For some time now, I am a Dancer contributor. It all started in the last Christmas (everything in the Perl community start or end at Christmas time), with Dancer Advent Calendar. Getting a little behind… I tried to learn to use Catalyst some time ago, reading a Packt Publishing book and found at that time that the book was already outdated. Unfortunately that is standard in Technology, and the books that keep up to date for some time are the ones that focus the foundations of computer science, and not the technology itself. It didn’t work. Probably I was not tuned to the MVC model, or I didn’t have a concrete project where to test Catalyst. Time passed, and I needed to put a website up. CGI was not an option. I do not think CGI is that bad, but if web frameworks exist, is because they lessen the work needed to make a website run. At that time I was pointing to Mojolicious, as I listened to a talk on a Portuguese Perl Workshop, and it seemed easier to learn than Catalyst. When I finally decided to learn a web framework I found that Dancer Advent Calendar, read the first two or three days (can’t remember exactly) and I liked it. I started to build a web site with it, and it felt good. With little effort I got the basics working. Of course problems arise, and I needed to visit #dancer on irc.perl.org for help. The users present on that channel at the time tried to help me. I confess they didn’t find the solution, as it was some weird problem on redirects and the fact that I needed to use Open3. That was the time when I needed to develop POSIX::Open3. But since them, I was hanging on #dancer. Listening to development discussion and newbies asking for help. I tried to help them as good as I could. Sometimes I needed to hack Dancer code to correct some bug or add a functionality requested by one of those newbies. And no, I wasn’t a Dancer developer then. Just trying to help. Accordingly with the Changelog, my first patches were released in Dancer 1.3001. I do not want to search on GitHub the exact date for my first pull request, but this release was out in January/February 2011. That is, only one or two months after I started using Dancer. The pull requests continue to pop in github. And I can be a hassle, poking the developers to merge my code :) About eleven releases after (a quick count, didn’t take care of what were stable or developer releases), Dancer developers started to name stable releases with names of somebody that helped/contributed a lot to that release. The first release of this kind was named after Michael Schwern, and the next was released after my name (1.3030, April 13th). About four months after I started using Dancer. As I did not stop to write code, fixing bugs and adding some new features, they made me a Dancer core developer in May 9th. I continue to help (I think) in the development of Dancer. And it all started with a simple site I needed to put online. As a final note, my thanks to Alexis Sukrieh, Damien Krotkine, David Precious, Franck Cuny and Sawyer X for putting up with me and for welcoming me to this community. ## Packt Publishing needs to learn typography I own some books from Packt Publishing and they are not bad. Of course, each book is different from each other, and some are better than the others. But that is normal that a publishing company that is trying to gain space in the market with serious competitors (like O’Reilly, Apress or Manning) needs to publish a lot, making it more difficult to publish good quality books. But after having a few of them, my main complain is not regarding the book contents, but the book design. It seems that Packt responsible people does not know typography nor marketing. Regarding marketing: try to make your books similar. Not only the cover, that you are already doing, but the inside too. They are not very different, but they are different enough to be a pain to read some of them, while other are pleasant to read. Regarding typography: accordingly with the table of contents of the books I am able to understand there is a chapter level and, inside each chapter, a section and subsection level. But unfortunately, when reading the book, the titles for sections and subsections are similar. If they have some difference, it should be one millimeter or so. That makes reading the book very difficult. The reader never knows if the subject is continuing or changing. You do not need to add numbers to the section and subsection titles, if you do not like them. But change the style! Make one of the titles underlined, or italic, or gray, or something! Oh, and finally, try to convince authors to make a good index. If your book is in a programming language, or a library, and examples use a specific keyword or function, please add it to the index, and point to the page where it is explained. That’s how users want to consult the index, not looking for the name of the example application. ## Book Review: Modern Perl I bought the Modern Perl book by chromatic. The title is inspiring. There is a lot of people writing Perl code as they were writing years ago. Although Perl still support those syntax and lack of strictness, there is a new trend on Perl code, and actual and future Perl developers should start learning it. The index of the book is inspiring as well. For one, it starts the chapter on object orientation with Moose and not with the old and crappy Perl OO system (in fact, I started learning Moose, and think there are too much Moose modules, too much documentation, too much of everything, that makes it impossible to find anything you would like). It could talk about Mouse or Moo, it does not matter, as only the basics are explained, and as far as I know, these three frameworks share the basic syntax and behavior. Also, the book as a good aspect when you look to it in graphical terms. It is quite easy to notice it is typeset with LaTeX and that is nice. And it is typeset with LaTeX using a set of Perl tools to convert from the POD (Perl documentation format) to LaTeX. Unfortunately the bibliographic entry on the book writes LaTeX in ASCII form, but it should render the correct logo for it (there is a command for that, you know?). The typesetting engine also changed the defaults from LaTeX. And, sorry for the language, they did shit. It is impossible to read a book where you cannot differentiate easily whats a chapter title, a section title or a subsection title. I almost feel tempted to measure the title lines and check how may millimeters there are of difference. More, the examples (and the full book, in fact) have the letters quite small, making it difficult to read. Enough for the format, now for the contents. After reading the old Learning Book and Programming Book written by Larry, Schwartz and Christiansen, the new versions with Tom Phoenix, and most recent books with brian d foy, I should say chromatic is in the bottom regarding read easiness and sense of humor. Yes, older books had much more sense of humor than current ones. The book is not for beginners. There are examples that are not complete. They have some text placed along with the example to explain it, but sometimes the most important piece to the beginning programmer is just missing. As an example, chromatic tries to explain that using$_ inside functions are bad, because you can’t use those functions safely on a for loop. He shows the for loop, shows the call for a function, but doesn’t show the function body. I think newbies will be kind of lost.

This said, I should congratulate chromatic and Onyx Neon for the courage and work publishing this book. Nevertheless, I think it needs some more work (both typographical and content related) and that a second edition is required as soon as possible.