Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to call a Perl subroutine from C and Java?

Posted on 2007-11-15
12
Medium Priority
?
674 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Accepted Solution

by:
Tintin earned 252 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 85

Assisted Solution

by:ozo
ozo earned 252 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 248 total points
ID: 20292955
0
 
LVL 92

Assisted Solution

by:objects
objects earned 248 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

972 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