Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Creating bash functions using perls $ENV interface

by sauoq (Abbot)
on Oct 29, 2003 at 03:11 UTC ( #302898=note: print w/replies, xml ) Need Help??

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 ./
and in
#!/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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://302898]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-03-31 01:25 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (364 votes). Check out past polls.