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
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
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
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!


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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Shell Script on AIX 7 172
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 99
Setting variables in a stored procedure 5 76
78 files, need to delete row 2 in every file 3 27
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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