aloknath has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have bunch of this bash scripts which has lot of functions.I wanted to call and use them in a perl script. I did something like this, but it does not works. I tried something like this :
Shell script : #!/bin/bash test() { echo "test:Inside tests.sh..." } export -f test Perl Script : #!/usr/bin/perl -w use strict ; print "Test main \n" ; system (("/bin/bash", "test")) ;
Its throwing this error : Test main /usr/bin/test: /usr/bin/test: cannot execute binary file Any idea how to do this ? Regards, Alok.

Replies are listed 'Best First'.
Re: Calling bash functions inside perl script
by roboticus (Chancellor) on Apr 04, 2007 at 11:08 UTC
    aloknath:

    A couple of things:

  • You're aware that test is already in bash, aren't you? You might want to use a different name so future maintainers don't assume they know what test is.
  • Calling functions from bash isn't going to pass it variable values. It may be better to get more familiar with perl by converting the functions from bash to perl.
  • With the given system command, bash will try to execute commands that it already knows about. So you'll need to figure out a way to preload your functions.

    Overall, it's probably too much work, and probably too error prone to attempt it. (At least from my perspective...) Again, converting from bash to perl would be a good exercise, and shouldn't be very difficult.

    ...roboticus

Re: Calling bash functions inside perl script
by kyle (Abbot) on Apr 04, 2007 at 11:02 UTC

    Part of your problem here is that there is a standard UNIX command called "test" (at /usr/bin/test, according to your error message). If you name your shell script something else, you might get a better error.

Re: Calling bash functions inside perl script
by lima1 (Curate) on Apr 04, 2007 at 11:06 UTC
    it is working when you source the file in your perl script:
    #!/usr/bin/perl -w use strict ; print "Test main \n" ; system( qq{ . test.sh ; testxxx } );
    test already exists in my path, so this is probably another error.