Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

From Perl to Java

by ropey (Hermit)
on Jul 24, 2008 at 16:01 UTC ( [id://699918]=perlquestion: print w/replies, xml ) Need Help??

ropey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks

When the company I worked for was taken over by a much larger one with Java as their core technology 2 years ago, I knew that the Perl app I have so lovingly developed would one day die a death.

But to my surprise after some initial misgivings the company realised that it was fast, stable and maintainable and could be coupled into the greater Java machine via XML and my world was good

However it has reached the point where new functionality is requested more and more rarely, and when it is the development is done 100 x faster than its Java big brother it is incorporated with

So I find myself with much less work than before, there is still a place for me in my job and there will always be the role of maintainer of the Perl side, however now I also am requested to get involved in the Java side as well

So for better or for worst I am getting involved, I am looking at the code... scratching my heads at times and realising how good Perl is to this beast... but the moral high ground isn't the question here. The question is what advice would anyone here give from moving from one language to another... its like a double whammy inheriting some code you didnt write and working out how it fits together as well as finding out the differences between languages. I find I start by looking at the few unit tests and following them, adding my own debug statements and following the flow of it.

However I always seem to find the differences annoying, for instance what is the equivalent to 'Data::Dumper' ? how do I do I 'split' a string what can do 'map' ?... These questions are Java centric and I am sure I could deep dive into the documentation, but is there some sort of perl - java mapping out there handy ?

Apart from above has anyone else been through this ? I know its going to be a learning curve, but other monks previous journeys may make my own journey that little bit less painful

Replies are listed 'Best First'.
Re: From Perl to Java
by crashtest (Curate) on Jul 24, 2008 at 17:49 UTC

    My primary language of employment is Java, but it's been a long time since I was on the steep part of the learning curve. One thing I thought I'd throw out there would be to read the Javadoc both for the core api and any other libraries you might be using. It's the Java counterpart to perldoc. I think that would take you a long way.

    For example, string splitting? You might look at the documentation for java.lang.String and see it has a split method! Regexes in general? A search from the overview page would reveal the java.util.regex package.

    Yes, you're going to be typing a whole lot more :-( If Perl is like Charles Dickins' prose in its expressiveness, Java is more like a third-grader's paper on what they did last summer.

    As a general piece of advice, I would recommend imitating other code you can find in your project, or in reputable open-source libraries (say, part of the Apache Group). I think cargo-culting is your friend here, as long as you make sure you understand the code you're copying. This is just my opinion though, so if others have contrasting viewpoints, they might be more convincing.

Re: From Perl to Java
by almut (Canon) on Jul 24, 2008 at 18:57 UTC
    ...for instance what is the equivalent to 'Data::Dumper' ?

    Not exactly equivalent... but when I feel like needing Data::Dumper in Java (to get an idea of what some complex data structure contains), I usually use XStream with the JSON backend (which I find a little easier on my eyes than the default XML representation).

    The usual

    use Data::Dumper; ... print Dumper $data;

    then becomes something like

    import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver +; ... XStream dumper = new XStream(new JsonHierarchicalStreamDriver()); System.out.println(dumper.toXML(data));

    Not ideal, but better than nothing...

      I was hoping somebody would drop in with something like this. I knew my usual method of "Just make every object have a debug_print method that knows how to print itself in a sane way" was not ideal, but hadn't done more than a cursory search for something better. Thanks!

Re: From Perl to Java
by apl (Monsignor) on Jul 24, 2008 at 16:50 UTC
    I worked in a number of languages before Perl, so I had the opposite problem.

    If memory serves, Java supports hashes and regular expressions (my two favorite Perl features). So rewrite an existing Perl script in Java, and get used to the differences. And, sadly, be prepared to writing a lot more code than you used to.

    (Be grateful your employer is letting you cross-train. I'm in the middle of a corporate merger that is fairly brutal...)

Re: From Perl to Java
by Pancho (Pilgrim) on Jul 24, 2008 at 17:43 UTC
    I'm in a similar situation where I used Perl and then was required to use Java on some projects. What worked for me, and there are many approaches, is to learn Java the same way I learned Perl, through books... and not try to code Java using 'Perl Style'.

    It seems that you will be dong considerable work with Java so it might be good to put in the time and get a good foundation. Sun has some good (?) introductory tutorials. If learning through books is your thing a great book on effective Java programming is Bloch's Effective Java: Programming Language Guide, a new edition came out last month. There are also a number of intro books , one which is free and available online is Thinking in Java, this was good in my opinion for getting the language 'mindset'...

    Good luck...
    Pancho
Re: From Perl to Java
by TGI (Parson) on Jul 24, 2008 at 22:51 UTC

    If your Perl code is so important, you'd better ask to cross train an interested Java dev in Perl so that you aren't a single point of failure for your app.

    This arrangement should achieve several positive things:

    • provide you with an excellent opportunity to work closely with an experienced Java developer who can offer insights and guidance
    • spread the knowledge of Perl in your company, increasing the likelihood that Perl is used when appropriate
    • protect your employer from dependence on a single person for a system
    • save money by not shipping you off to a Java training class

    With your student/mentor person you can have thrilling conversations of the form:

    In source file Foo.pm, we are going to do X. In Perl we use the map() and split() functions to do X, blah blah blah. Now, how would you do that in Java?


    TGI says moo

Re: From Perl to Java
by Perlbotics (Archbishop) on Jul 24, 2008 at 21:01 UTC
    Some random, thoughts... It is a good thing to know (yet) another programming language. Perl has strengths, Java has strengths. That gives one the opportunity to chose the language that is better suited for the job at hand (another tool in the box). I would not try to map Perl2Java (analogue: see perlsyn '...a C programmer might...'). Each language has its own idioms. You can profit from learning Java the Java-way (OO etc.). Agreed, usually Java means somewhat more typing compared to Perl (in other words: more time spend until the coding bliss kicks in ;-), but on the other hand that is often compensated by frameworks (e.g. Tomcat for servlets, JUnit for testing, etc.) and faster execution speed. The online Java documentation is quite good and extensive. Besides buying a book, this is an opportunity to ask your boss for a training or even a Java certification (set of trainings).

      Any proofs regarding the execution speed difference? I bet there was a lot of improvement over the years in the Java world from the notoriously insanely unusably fat and slow language of late 90s, but there is very little reason for Java to ever be quicker than Perl.

        but there is very little reason for Java to ever bee quicker than Perl.

        I guess with JIT compilation and smart optimizers you can get very good speed improvements, inline method calls and stuff like that. Since perl 5 has not JIT, many possible optimizations can't be made.

        But I don't know which is actually faster in what cases. Maybe the Debian language shootout has some answers, but at the moment I'm too tired to interpret the results ;-)

Re: From Perl to Java
by dHarry (Abbot) on Jul 25, 2008 at 07:13 UTC
Re: From Perl to Java
by whakka (Hermit) on Jul 25, 2008 at 04:53 UTC
    A lot of Comp Sci programs offer their course materials online for free. I would recommend U. Penn's materials and download the lectures for 120 and 121. I used them a lot to get going.

    Other than that visit the API and Sun's Tutorials early and often.

Re: From Perl to Java
by ggvaidya (Pilgrim) on Jul 25, 2008 at 12:18 UTC

    Don't worry about it - it's just another language! I'd just like to second the oft-repeated suggestion that you shouldn't write Perl in Java - from personal experience, you'll end up with very messy datastructures which can't be manipulated as easily as they can in Perl. That way lies frustration and hairpulling. Try and pick up the Java idioms, and to really embrace object-oriented programming: it'll make you a better Perl programmer.

    I've found that using Google to search directly against the Java API is really good for finding exactly the right method to do something. You might have to try searching by what you want to get done, rather than the name you think the method might have - Java doesn't believe in TMTOWTDI! - and sometimes you'll have to roll your own.

    All the best!

      Don't worry, it's just another car. Who cares that it doesn't have the automatic shifting you are used to, that it doesn't tell you when the oil or gas is low, when the engine is overheating, that to start it you have to use a crank ...

      If you don't already type with all ten, get a course of that, it's gonna pay off.

Re: From Perl to Java
by Mutant (Priest) on Jul 25, 2008 at 08:32 UTC

    Can't say I have a lot of experience with Java, but I'm hoping to take the headlong plunge shortly, as most of the interesting work at my job is in Java these days (seems to be a bit of a trend...)

    A couple of things though - as most others have already said, Java is a fairly easy language to learn. Static typing can be annoying, and you sometimes miss a few of the more powerful idioms of Perl, but that passes quickly. I also sometimes have to remind myself that where I'd normally reach for a data structure in Perl, that Java Way is to use an object hierarchy. (I think it's important to learn the "native" way of coding in a language - I've seen plenty of bad Perl code written by C programmers).

    Aside from the language, the learning curve is more about all the tools around Java. You don't *have* to use an IDE, but the vast majority of Java devs do. You might also have to deal with things like Maven, Hudson, Spring, Hibernate, Tomcat and whatever the Flavour of the Month is. On top of that, you might have to get your head around SOA, ESBs, Service Orchestration and Choreography, Beans, POJOs et al. (After a while you might realise Java people like to give important sounding names to things that are relatively simple concepts, albeit with a lot of complexity in the details).

    Anyway, have fun :)

Re: From Perl to Java
by explorer (Chaplain) on Jul 24, 2008 at 21:37 UTC
Re: From Perl to Java
by DrHyde (Prior) on Jul 25, 2008 at 10:49 UTC
    How did you find out about Data::Dumper, split and map in perl? If you're anything like me, you found them by a combination of reading other peoples' code, going through tutorials, and asking people who knew more perl than I did at the time. Well, that's how I'd approach learning Java too.
Re: From Perl to Java
by beryan (Scribe) on Jul 25, 2008 at 04:56 UTC
    Don't try to find equivalences. Just as you learn more from another languaje you will find that there are cool things that you didn't know and are good and useful in the other, some are the same, and other are crappy.
Re: From Perl to Java
by Lori713 (Pilgrim) on Jul 31, 2008 at 19:39 UTC
    Just chiming in...

    I, too, have this looming over my head for the future and will be following this node closely when I'm finally given my first Java app to write.

    My employer signed me up for an intro Java class, which I attended about 18 months ago. For various reasons, other Perl projects kept getting priority over the incoming Java project, so I suspect I've lost what little I was able to learn. I don't think there's a broom big enough to knock the cobwebs off those snippets of knowledge floating around in my head.

    One of the most difficult aspects of the class (and actually gave me a headache by the third day), was trying to make my brain go from procedural (I think that's the right word) to object-oriented. For me, that was the biggest challenge -- figuring out the syntax to accomplish what I wanted. Hmmm... that may be why Javascript is still a challenge for me.

    I'll check out the sources and suggestions from the other responders to see if there are helpful guides on how to tweak the brain into a different style of programming. :-)

    Good luck ropey! Lori

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699918]
Approved by Arunbear
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found