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.


In reply to Re: Sourcing cshrc environment in Perl by Dominus
in thread Sourcing cshrc environment in Perl by Kirby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.