Solved

How to call a Perl subroutine from C and Java?

Posted on 2007-11-15
12
667 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 56
compre toata in where clue oracle 4 56
oracle 11g 23 50
varialbe initialization 11 30
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now