Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Is Inline::Java suitable for groovy

by xiaoyafeng (Chaplain)
on Jan 23, 2019 at 04:09 UTC ( #1228852=perlquestion: print w/replies, xml ) Need Help??
xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

Recently I took over a legacy web application written in groovy. I've heard that groovy is an easy and friendly java, and I noticed that the code uses jdbc to connect mysql. So is there a way to make use of the class of it from perl?

Thanks in advance!

UPDATE: with monks help, I've successfully called groovy class in perl by Inline::Java. But markong's comment make me rethink. Is my first thought a good idea?





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: Is Inline::Java suitable for Groovy?
by Athanasius (Bishop) on Jan 23, 2019 at 06:38 UTC

    Hello xiaoyafeng,

    I have no experience with Groovy, and I don’t know whether Inline::Java can be adapted to support it.

    But in a search for “Groovy” on MetaCPAN the first result is FFI::Platypus, which is is designed to “Write Perl bindings to non-Perl libraries with FFI. No XS required.” In the EXAMPLES section of the documentation for that module, the Java subsection says this (underlining added):

    Discussion: You can't call Java .class files directly from FFI / Platypus, but you can compile Java source and .class files into a shared library using the GNU Java Compiler gcj. Because we are calling Java functions from a program (Perl!) that was not started from a Java main() we have to initialize the Java runtime ourselves...
    ...
    ...this enables you to call Java from Perl and potentially other Java based languages such as Scala, Groovy or JRuby.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks your reply!

      This is a new way that I've never thought of. But after I did some research of gcj, I think it's not suitable for my scenario. Since it actually convert the codes 2 times for using( java 2 c, c 2 perl) it will take much time to diagnosis when any issues happen.

      But I'll try this way on vacation. ;) Thanks




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Re: Is Inline::Java suitable for groovy
by afoken (Abbot) on Jan 23, 2019 at 07:55 UTC
    I notice that the code use jdbc to connect mysql. So is there a way to make use of the class of it from perl?

    I may understand you wrong. If so, please ignore this posting.

    There is no need to go through Java to connect to MySQL. Perl has at least three ways to do so natively, all start with DBI. The native one is DBD::mysql, but that driver has collected several issues over time. DBD::MariaDB is a fork of DBD::mysql, like MariaDB is a fork of MySQL. DBD::MariaDB can connect to MySQL. The third way is to use DBD::ODBC together with an ODBC manager (comes out of the box with Windows) and an ODBC driver. This has significantly more overhead than the two native drivers.

    If you like JDBC, there is also DBD::JDBC that can be used to connect to any database supported by Java, using Java drivers. Of course, this has at least as much overhead as ODBC.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      eh, I mean that because the code of groovy use jdbc to connect db, I suppose I can call groovy from Inline::Java)




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Re: Is Inline::Java suitable for groovy
by markong (Monk) on Jan 24, 2019 at 23:58 UTC

    I would say that yes, on paper you can: I haven't tried directly but remember that Groovy is one of the very few languages to retain a seamless integration with the Java Runtime Platform: it runs inside the JVM and makes use of Java’s libraries (JRE). Groovy is only a new way of creating ordinary Java classes — from a runtime perspective, Groovy is Java with an additional JAR file as a dependency.

    Thus you can write a Java wrapper for the groovy classes onto which Inline::Java can plug in.

    That said: Groovy is a sort of Perl for the JVM, it's pretty powerful/feature rich, unless you have already a vast of Perl 5 code which need the Groovy script/program functionality, I'd recommend stick with it: it also supports so-called "slashy" // regular expression literals (I'll let you guess where they have "stolen" the idea) among loads of other very nice feats.

      Many Thanks! I decide to do on the way that you point out.




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2019-03-26 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (124 votes). Check out past polls.

    Notices?