The typical solution is something like this:
open ENV, "csh /van/home/.cshrc; env |" or die ...;
while (<ENV>) {
chomp;
my ($var, $val) = split /=/, $_ , 2;
$ENV{$var} = $val;
}
What's the idea here? You must run a sub-process
to read the .cshrc file, because to read a
.cshrc file, you have to run csh. So the
sub-process is mandatory. But then you get the subprocess to
spit out its environment, and you load in all the appropriate
settings.
The only problem with this is that sometimes environment variables can contain
newlines or other funny characters, in which case you may not
be able to parse the output of the env command
reliably. The easy way around this is to use your own
reliable replacement for env instead of the
real env. This is very easy:
# myenv
for my $var (keys %ENV) {
print $var, "=", $ENV{$var}, "\0";
}
Now the variables are terminated by \0 instead of
\n, and to parse the environment data back in the
main program, just use local $/ = "\0" when
reading the output of myenv.
This same trick works for /bin/sh scripts also,
of course.
|