How to call a function (defined in shell script) in a Perl script

  • A+

I have two scripts, namely and : It has function definitions which, when invoked from Perl script, will execute certain commands on Linux in batch mode. : It has the code and logic to be implemented, which functions to invoke etc.

The content of file is as under:

bash-4.2$ cat  #!/bin/bash  # Function Definitions func_1 () {   echo "function definition" }  func_2 () {   echo "function definition" } 

The content of file is as under:

bash-4.2$ cat  #!/usr/bin/perl  use Getopt::Long;  my $var1; my $var1;  GetOptions("var1=s"     => /$var1,        "var2=s" => /$var2,        "help|?" => /$help );  if ($help) {     HelpMenu(); }  print " Can I call the func_1 (defined in shell script) with arguments here..?? (in this perl script)"; 

How can i invoke the function func_1() (which is defined in in the perl script ?


To invoke a shell function, the shell needs to know its definition. One way to achieve that is to have the shell first source the file which defines the function. And then, without exiting the shell, call the function. From a Perl script, that would be for example:

system 'bash', '-c', 'source; func_1'; 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: