Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Java programming and Perl

by heatblazer (Scribe)
on Dec 03, 2012 at 18:28 UTC ( #1006922=perlquestion: print w/replies, xml ) Need Help??
heatblazer has asked for the wisdom of the Perl Monks concerning the following question:

Hello, all

I `ve heard rumors that Perl can be glued to other programming languages such as C++. I am interested how can this be done in Java, so I can use some tools from Perl I can`t normally use from Java, also, can I pack it into the jar file, so, let`s say, I can use a java program with Perl bindings on Android device?

I`d really appreciate some small examples and explanations.


Here is an example in Java with it`s defaults scripting engine(JS).

import javax.script.*; import*; class Jsc { public static void main(String[] args) { try { ScriptEngine engine = new ScriptEngineManager().getEngineByName("jav +ascript"); for ( String arg : args ) { FileReader fr = new FileReader(arg); engine.eval(fr); } } catch (IOException ioEX) { ioEX.printStackTrace(); } catch (ScriptException scrEX) { scrEX.printStackTrace(); } } //end of main } //end of class

Updated Passing a .js script file will result in executing it, also the js can interact with the program if programmed. Perl has Java modules from CPan, in Java cookbook by F.Darwin there is a topic: "Marrying Perl and Java" which gives a partial answer to my desire, but - it`s going from Perl, I need something as above code but with perls scripts so like I can program my java app with a small perl scirpt, let`s say perl script will search for files, java will read and execute them.(Just example, I won`t do exactly this).

I wonder if it can be done with Perl?

Replies are listed 'Best First'.
Re: Java programming and Perl
by tobyink (Abbot) on Dec 03, 2012 at 18:36 UTC

    It's possible to call Java code from Perl using Inline::Java. An example module that does so is XML::Saxon::XSLT2.

    There are similar inline modules for other programming languages - Inline::C, Inline::Ruby, Inline::Lua, etc.

    The other way around - calling Perl from other programming languages - is also possible. Perl can be compiled as a library ("libperl") which can be embedded into C programs; and thus theoretically be called from any other programming language capable of linking against C libraries. There exists a PECL module allowing Perl to be embedded in PHP.

    I don't think this would be a fruitful or indeed sane way of getting Perl running on Android though. Maybe look at perldroid?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      So let`s say I can make a libperl then call it like a C code with java native interface?

        Yes - as I understand it, this is what perldroid does.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Java programming and Perl
by ColonelPanic (Friar) on Dec 04, 2012 at 08:32 UTC

    Why is this not sane?

    Of course you can do whatever you want, but why might this not be a good idea? Perl is quite large. The entire Perl interpreter is required to run any piece of Perl code. Thus, if your app requires Perl, it will take up a huge amount of storage space. Furthermore, it will probably be quite processor-intensive to run. Java is already expensive to run, and you will be adding the Perl executable on top of that.

    Of course, many (most? (all?)) tasks are much easier to accomplish in Perl than Java. That being said, Java can do pretty much anything. It may be more annoying to write it in Java, but it is almost certainly still preferable to stick with one widely available environment for your project. The cost of adding Perl is just too high on a mobile device with limited resources.

    When's the last time you used duct tape on a duct? --Larry Wall

      Well, I agree with that PerlInterpreter will cause a size overload, but as I read now, Java`s default embed scripting language for end users is Rhino and it`s a javascript written in java. I was interested in Perl as end user script language instead of JS, because Perl is much more powerful and flexible, with CPan and the textual process power, so using Perl is definitely > than JavaScript. I don`t know if size overload is greater in priority than lose of functionality.

        Your curiosity is a desirable attribute. But let me make a suggestion. I've embedded Perl interpreters into C and C++ projects before. It's not terribly difficult to get something simple up and running. A basic framework is found in perlembed. However, it can get very complex, very fast.

        My suggestion is this: Read perlembed thoroughly, and then get an interpreter embedded in a C program. Put it through its paces with C first, before ever considering moving onto the next step of exposing the C code to Java. In particular (since you mention the power of CPAN), work on getting the embedded interpreter to work with CPAN distributions that you think will be used by your Franken-Java creation.

        Interfacing Perl with other languages at an 'internals' level is one of those pursuits where it's best to learn to walk before trying to run. When you look down you'll notice that you're skirting a cliff. ;)


        I do have to admit, there is definitely a "cool factor" to using Perl on Android. As long as you are just experiementing, go ahead and have fun!

        Still, if you plan to release your app to others, you have to think about the costs. This will add at least several megabytes to your project. You will also be using a build of Perl that is experimental.

        This is not about the superiority of Perl to Javascript--I agree 100% that Perl is a better language. This is about the appropriateness of the tool for your task. Perl on Android would almost never be a good choice for a production app.

        When's the last time you used duct tape on a duct? --Larry Wall
Re: Java programming and Perl
by Faern (Initiate) on Dec 04, 2012 at 18:48 UTC
      FYI, the Perl-Android-APK project is coded by independent programmers, not by Google itself - it's just hosted on Google Project Hosting.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006922]
Front-paged by Arunbear
[shmem]: Lady_Aleena: fix it, or delete it ;-)
[Lady_Aleena]: shmem, I'm not sure how.
[shmem]: well, deleting is done with 'rm' :-P
[shmem]: you can break up the chain MP3::Tag->new($_)- >interpolate("\%S ") and test whether MP3::Tag->new($_) is true
[Lady_Aleena]: shmem, no to deletion.
[shmem]: can't help you there since that song is not on my bench
[Lady_Aleena]: shmem, I need to look at MP3::Tag to see how to get the date from one song.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-04-23 22:14 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.