Solved

How to call a Perl subroutine from C and Java?

Posted on 2007-11-15
12
666 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
#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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 62 total points
Comment Utility
0
 
LVL 92

Assisted Solution

by:objects
objects earned 62 total points
Comment Utility

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
Comment Utility
for the returnCode, would it be able to return a string value as well?
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
My examples return the string value.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

10 Experts available now in Live!

Get 1:1 Help Now