http://qs321.pair.com?node_id=563286


in reply to A Perl vs. Java fight brews

I'll state up front that I would choose Perl for the task you've outlined. But then I'm responding to a post on perlmonks, which might reveal a little bias.

One question I'd ask you is: do any of your colleagues have Java experience? If nobody has any Java experience but one member of the team is proficient in Perl, then that makes a big difference. If on the other hand 4 out of 5 team members have used Java, it's going to be a lot tougher to sell Perl.

greatly reduce the spawning of new ksh processes
If I understand the requirements correctly, I think Perl would be a better choice in this area because it's possible to do a lot of shell type operations directly in Perl code.
ready interface to unix system services
Similar to the above, Perl is much more "unix-ish" than Java. An example of something that is much more difficult in Java is manipulating file permissions. There is no chmod in Java.
Open3 to control input and output between existing utility programs
If you mean using pipes, I can't comment because I've never done that with Java. What I can say is that file I/O is a lot more work in Java compared to Perl. In Java you don't just open a file and print $fh "foo", you have to deal with a bunch of confusing APIs. Similarly, there is no @lines = <$fh> in Java, and reading from files involves more, different, confusing APIs.
CPAN
This is a clear win for Perl. There is no equivalent repository for Java. I think the amount and quality of available Java libraries has increased a lot in the past few years but it still takes a lot more time to find and evaluate the options.
associative arrays
There are several hash-type structures available in Java. You'll find them harder to work with than Perl's hash though. First, you need to decide whether you need a Map, HashMap, Hashtable, TreeMap, etc. You can store just about anything in these structures, as long as it's an object. But then when you go to get something out, you'll need to cast it back to the proper type of object before you can do anything with it. Then there are the sorting issues. Some of these collections can't be sorted, period. Others can be sorted, but if you want to sort in an order different from the "natural order", you'll need to write your own Comparator class. This is a great example of where I gripe that I wish I was using Perl instead.
scoped untyped variables
Scoped, yes. Untyped, sorry, Java don't do that.
flexible choice of procedural versus OO (ksh is only procedural)
Sorry, Java is OO only.
perl -d (debugger - no such thing for ksh)
I've never used the Java command-line debugger, but if you use an IDE like Eclipse, the debugging is actually pretty cool.
perl's regexp engine is better than awk/sed's without needing to shell out
Starting with JDK 1.4, Java has native regex support. There are also some third party implementations like Jakarta ORO and Jakarta Regexp. Like most things in java, using regular expressions requires more code than in Perl. I recommend Mastering Regular Expressions, it has good coverage of the available options for Java. Also, I've found cases where I was unable to port a regex that worked in Perl to any of these.
perl's hashes are more flexible and scopable than formula engine's (Formula Engine provides only a single global hash).
See my comments above on associative arrays.
perl can communicate more effectively with background processes.
I'm not sure exactly what you mean here. If you mean getting information about processes that you didn't spawn, the Java security model will probably make this difficult.

Replies are listed 'Best First'.
Re^2: A Perl vs. Java fight brews
by Moron (Curate) on Jul 24, 2006 at 15:08 UTC
    Thanks for your insightful comments. In regard to communicating with background processes, some related Perl coding examples of what I mean can be found in the Interprocess Communication chapter of Programming Perl (3rd edition) by Larry Wall - can Java communicate this flexibly with its children? Does it have forks, threads, etc. anyway?

    -M

    Free your mind

      Thread support in Java is pretty robust, and I think that's the way you'd handle this type of problem. Rather than forking children and communicating with them, in Java you'd use threads to do the work.

      If you really need processes, this could be a big selling point in favour of Perl.

      Java fork emulation is a real pain in the ass: you basically have no easy way to fork the current process, so you have to call Runtime.exec() and pass it the Java class to execute. This fires up a new JVM, which takes a lot of CPU and memory. Then, you'll also have major problems in communicating between the various JVMs, since Java does not (natively) offer anything like pipe or the inter-process communication tools offered by the various IPC::* modules, so you basically have to resort to JNI, or you have to use sockets, RMI, or even CORBA (one can also resort to EJB, that's true, but this could be an overkill and an unmistakable source of major headaches).

      As for threads, though it's true that Java prefers threads over real processes (and their implementation in Java is robust), it is important to stress that threads are not real processes.
      For instance, real processes will seamlessly migrate over an SSI cluster of machines (as long as they don't use shared memory), while threads will not.
      Also, an application based on real processes rather than on threads is much more robust, since each process runs in a separate address space while threads all live in the same address space, so that a single thread crash can bring down the whole application.
      And forking a new process is only marginally slower than creating a new thread, on systems that use Copy-On-Write (basically any modern *nix).

      Ciao, Emanuele.
Re^2: A Perl vs. Java fight brews
by ikegami (Patriarch) on Jul 24, 2006 at 17:45 UTC

    Sorry, Java is OO only.

    I keep hearing this, but it makes no sense. A class filled with static members and methods is no different than a package filled with global variables and functions. Perl uses a methods as subs approach. Java uses a subs as methods approach. The syntax is different, but both are capable of procedural and OO programming.

      Yes, you can make all static classes and put a driving script in a main method in Java. But, not matter what, you must put all of that in a class. Hence the charge: Java is Object Oriented Only. We say this, because every single line of operative code must be in a method and every single method must be explicityly placed into a class. You can't just write a driver:
      print "driver starting\n"; `call_system_util1`; `call_system_util2`; print "driver finished\n";
      The equivalent code in Java must be in a method of some class. That's overkill in this case and it buys nothing except tedious typing, which is why we make the charge and it sticks.

      There are two subtle bits of syntatic sugar here. First, Perl puts things in the main package by default. Second, code does not have to appear in a method/sub/function/whatever. It can just go in a driving script. Perhaps these differences seem small, but they are real.

      Phil

        But, not matter what, you must put all of that in a class. Hence the charge: Java is Object Oriented Only.

        Same in Perl. If one is not specified explicitely, main is used.

        You can't just write a driver

        You can't have driver code in C either, but that doesn't stop you from coding procedurally in C. That Java can't have drive code does not preclude programmers from writting procedural Java programs.

        Perhaps these differences seem small, but they are real.

        I didn't say there weren't differences. (Quite the opposite.) I said programmers code procedurally in Java, so it's not only OO.