Solved

How to call a Perl subroutine from C and Java?

Posted on 2007-11-15
12
671 Views
Last Modified: 2012-08-13
Hi,

I have a perl script in the following format.  It defines a package crypto, and has function decrypt.

package crypto;

sub decrypt
{
   ...
   my retval = "String";
   return retval;
}

Decrypt, returns the plain_text string of whatever it was told to decrypt.

Now, my problem is that how can I call this function from JAVA and C, getting the return value back in both cases?

Any suggestions?  Thanks in advance.
0
Comment
Question by:Kushal_Agarwal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 20291814
You still haven't given a good reason as to why you can't have a Perl script wrapper or why you need this subroutine in a package.  It would certainly make your life a lot easier.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20291915
You can always invoke the perl interpreter using Runtime.exec. See http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 

Author Comment

by:Kushal_Agarwal
ID: 20292052
What do you mean by a perl script wrapper?
I don't care if this subrouting is in a package or not, as long as I can invoke it from Shell scripts, Java and C, getting the return value in each case.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 48

Accepted Solution

by:
Tintin earned 63 total points
ID: 20292552
In that case, you can make life *so* much easier, by changing it to:

#!/usr/bin/perl
my $string = shift;
# decrypt code here
print $decrypt;

Then in your shell script, Java and C programs, you simply call the perl script, eg:

In shell

decrypt=`/path/to/script.pl ABC`

In C/Java, just use the equivalent call.

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 63 total points
ID: 20292905
#include <stdio.h>
main(){
  char value[100];
  FILE *P = popen("perl -Mscript -e 'print crypto::decrypt'","r");
  fgets(value,100,P);
  pclose(P);
  printf("value=%s\n",value);
}
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 62 total points
ID: 20292955
0
 
LVL 92

Assisted Solution

by:objects
objects earned 62 total points
ID: 20293231

Process p = Runtime.getRuntime().exec(new String[] {"/usr/bin/perl", "scriptname.pl" });
// or if you don;t use a wrapper something like:
// = Runtime.getRuntime().exec(new String[] {"/usr/bin/perl","-Mscript","-e", "'print crypto::decrypt'"});
p.waitFor();
int returnCode = p.exitValue();
0
 

Author Comment

by:Kushal_Agarwal
ID: 20294434
for the returnCode, would it be able to return a string value as well?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20294516
My examples return the string value.
0
 
LVL 92

Expert Comment

by:objects
ID: 20294591
you can read output from stdout/stderr using getInputStream() and getErrorStream() methods of Process class

http://exampledepot.com/egs/java.lang/ReadFromCommand.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question