Solved

How to invoke a perl function from a shell sciprt and C code, and get a string back as the return value?

Posted on 2007-11-14
7
806 Views
Last Modified: 2010-05-18
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 a Shell script and C code, getting the return value back in both cases?

Any suggestions?  Thanks in advance.
0
Comment
Question by:Kushal_Agarwal
7 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 20285590
From a shell script:

string=`perl -Mcrypto -e 'print crypto::decrypt("abc")'`

But why don't you just have a calling Perl script that takes the args?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20285594
eg:

#!/usr/bin/perl
use crypto;
my $string = shift;
print crypto::decrypt($string);
0
 
LVL 84

Expert Comment

by:ozo
ID: 20285614
#!/bin/sh
cat <<'END' > script.pm
package crypto;

sub decrypt
{my $retval = "String";
   return $retval;
}
1;
END
value=`perl -Mscript -e 'print crypto::decrypt'`
echo $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.

 

Author Comment

by:Kushal_Agarwal
ID: 20289245
Tintin,
I thought about that, but the problem is that its other people who have the shell scripts, and they are not too excited about changing them to perl scripts.

Now when I invoke the function from the shell script like you mentioned, how do the parameters get passed, meaning are they going to be visible in the process tree?
0
 

Author Comment

by:Kushal_Agarwal
ID: 20289251
Also, how can I do the same thing from C code?
0
 

Author Comment

by:Kushal_Agarwal
ID: 20289281
Ozo,

To call the perl function decrypt, don't I just need the line
value=`perl -Mscript -e 'print crypto::decrypt'`

What is everything above it for?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20291001
> .. re they going to be visible in the process tree?
yes (at least for the examples as shown above)
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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

947 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

23 Experts available now in Live!

Get 1:1 Help Now