Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Source ksh from Perl 4

Status
Not open for further replies.

wesbooks

Programmer
Sep 8, 2002
22
CA
Hello expert,

I have a ksh exporting all variables. And I want the Perl script to use it. I tried System() but no luck. How can I source ksh from Perl?
 
If the Perl script is run from the ksh script, the Perl script should be able to see any exported variables in the $ENV hash. If that doesn't tell you enough, please post relevant portions of your code.

 
To amplify, after a bit of experimentation:

1. ksh doesn't have an actual source command. bash has one, and I believe csh does too, but the ksh equivalent is simply a dot. This reads and executes commands from the file named after the dot in the current shell environment.

2. Using system in the Perl script to execute the file you want to source won't work because the file will be executed in a separate shell environment, not the one in which the Perl script is currently executing.

So I have in mind something like this:
Code:
#!ksh
...
[b]. sourcefile.ksh [args]
...
perl perlfile.pl [args]
If you do it this way, perlfile.pl will be able to access any vars exported by sourcefile.ksh in the %ENV hash, i.e., $ENV{varname}, where varname is the name of a variable exported by sourcefile.ksh.

I hope this clarifies what I was trying to say earlier.

HTH
 
Hello Mike,

It is great info. However, my company didn't want to have the perl called form ksh. They wants the perl executed all by itself.

In that case, I don't think Perl can share export valuables if it is written in ksh.
 
There's no way I can think of that the Perl script can execute the ksh script and have the Perl script see the variables the ksh exports. The env vars will have to be set when the p.s. starts up for the p.s. to see them.

I don't know if it matters now, but there was a typo in my last post. Should be
Code:
#!ksh
...
[b]. sourcefile.ksh [args][/b]
...
perl perlfile.pl [args]


 
if the ksh script can be modified, and depending on what it might already print, insert
env
into the ksh script and have the perl script read the ksh output
 
Hello Arnold,

Thank you for your reply.

Can you provide some sample codes how to use ENV in ksh? I tried "export" but no luck.
 
try this is an example
tryenv.sh=
#!/bin/ksh
env

perl script=
open ENV,"./tryenv.sh|";
while (<ENV>){
print $_; #debug
stuff
}

now, all you need do is code stuff that transforms the $_ into
an eval'able $ENV{ } expression.
 
You can also try Paul Johnson's Shell::Source module:


You can use it something like this:

#!/bin/perl

use Shell::Source;
my $ksh;
$ksh = Shell::Source->new(shell => "ksh", file=>"/some/shell/script.sh");
$ksh->inherit;

Any parameters exported from the KSH script will be added to your Perl scripts' %ENV. If you need to parms declared by not exported from the shell script, you can override the ksh invocation in Shell::Source->new() to include a -a parameter.
 
Outstanding, apostate. I tried this after reading your post and it does indeed seem to work. Have a star.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top