Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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 ./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.";


Comment on Re: Creating bash functions using perls $ENV interface
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://302898]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (20)
As of 2015-07-31 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (278 votes), past polls