http://www.perlmonks.org?node_id=302898


in reply to Creating bash functions using perls $ENV interface

Update: Actually....

perl -le '$ENV{sauoq}="() { echo sauoq; }"; print `bash -c sauoq`'
:-) (And suddenly it occurred to me that export -f had to work somehow.)


The following was my original reply. When I updated this node after finding a Real Answer™ for the OP, I struck this out. The suggestions below aren't wrong though and someone may still find them useful; so I'm unstriking them.

As far as I know, there's no way to do exactly what you are asking for. Every process has an environment, and yes, perl gives you access to its with %ENV; but not every process has bash functions... if you see what I mean.

There are ways you might be able to get around it. You can call your perl script from a bash script which itself declares and exports the function you want available to the shells started from your perl script. Something like this:

#!/bin/bash sub sauoq() { echo sauoq } export -f sauoq ./script.pl
and in script.pl:
#!/usr/bin/perl print `bash -c sauoq`

Or, you might try opening a pipe to a bash process, writing the functions out to that pipe and keeping it open to run your other scripts.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Creating bash functions using perls $ENV interface
by tid (Beadle) on Oct 30, 2003 at 00:20 UTC

    Heya sauoq,

    Thanks for taking the time to check it out. I just had to delete a whole bunch of response text, as I finally got it to work under cygwin using the following script:

    #!perl -w $CYGWIN_BIN="C:\\cygwin\\bin"; $ENV{TEST_FUNC} = "() {\necho Hey, it works!;\n };\n"; system(("$CYGWIN_BIN\\bash.exe", "-c \"declare -f\"")); system(("$CYGWIN_BIN\\bash.exe", "-c \"TEST_FUNC\""));

    Initially, using your
    perl -le '$ENV{sauoq}="() { echo sauoq; }"; print `bash -c sauoq`'
    I had problems where bash didn't understand what sauoq was. However, the script above does the trick

    Thanks to everyone for the interest guys (especially sauoq) - you guys saved my butt!
    Mike