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
Last Modified: 2010-05-18

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.
Question by:Kushal_Agarwal
LVL 48

Accepted Solution

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?
LVL 48

Expert Comment

ID: 20285594

use crypto;
my $string = shift;
print crypto::decrypt($string);
LVL 84

Expert Comment

ID: 20285614
cat <<'END' >
package crypto;

sub decrypt
{my $retval = "String";
   return $retval;
value=`perl -Mscript -e 'print crypto::decrypt'`
echo $value
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Author Comment

ID: 20289245
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?

Author Comment

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

Author Comment

ID: 20289281

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?
LVL 51

Expert Comment

ID: 20291001
> .. re they going to be visible in the process tree?
yes (at least for the examples as shown above)

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

792 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