Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to call a Perl subroutine from C and Java?

Posted on 2007-11-15
12
Medium Priority
?
672 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
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 84

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

671 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