Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call Perl functions from Shell script? 1

Status
Not open for further replies.

volcano

Programmer
Aug 29, 2000
136
HK
Hi, I have a PERL program, say "abclib.pl" which contains a function named "CONVERT". How can I call this function in my shell script "abc.sh"? The function requires an argument as input and returns a string. I would appreciate it if you could give me some hints.
 
You could write a perl program that loads your perl library and as main program only drives your function, and then call that program from the shell script.

But you'll end up with re-interpreting the perl program (and your library) everytime you want to run the function. Not really desirable.

Perhaps write your whole shell program in perl. Or write a shell function to replace the perl function. What exactly does the program do? And the perl CONVERT function?




HTH,

p5wizard
 
hi, thanks for your reply. In fact that PERL program is a third party program downloaded from It provides a function to return the Chinese encoding from an input string/file. Therefore I have to call its function from a shell script. Any idea?
 
Hi

If you do not need that file as is, rename it to abclib.pm, put an [tt][highlight white]1;[/highlight][/tt] at the end of file, then :
Code:
perl -Mabclib -e "CONVERT(argument)"

Feherke.
 
so if you have your string in var1 in your shell script

Code:
var="text_to_be_converted"
perl -Mabclib -e "CONVERT(${var})"

provided you modify the perl file as per feherke's suggestion

HTH,

p5wizard
 
Hi, so wonderful to have such simple way to do it! BTW, do you mean I should add "1;" (exclude double quotes) at end of the abclib.pm? May I know why I have to do so? Thank you.
 
Hi

In facts, the [tt]1;[/tt] is needed if you add [tt][highlight white]package abclib;[/highlight][/tt] too, at the begin of the file. In this case a return value is required. But in this case I think will work without [tt]package[/tt] and [tt]1[/tt] too.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top