shreedarasp has asked for the wisdom of the Perl Monks concerning the following question:

i have one third party application which generates its own environment variables and inherits its parent ENV variable. this third party application can also run perl code. my perl code and the application s/w runs independently and i need to access some of the env variables generated by application s/w. but i was unable to get the env vars generated by appln s/w from my perl code. is there a way in perl to get the env variables generated by appln s/w may be by giving porcess ID or appln name?
note that the env vars created by appln s/w keeps changing.
i found one work around method by writing those env vars to a file from the appln s/w and then source it on my perl code. the problem with this is, the nature of the var keeps changing and i have created a button so it changes the value. is there a way to run another perl code automaticaly when the file gets generated at perticular location? in other words from my perl code i need to run another perl code ONLY when one perticular file gets generated at perticular location? I am running on windows XP and i need to run this code both on Win XP and RHEL 5
  • Comment on Getting environment variable of a process

Replies are listed 'Best First'.
Re: Getting environment variable of a process
by cdarke (Prior) on Dec 30, 2008 at 14:28 UTC
    my perl code and the application s/w runs independently

    By "independently" I assume that the application is not the parent of your perl code. If that is the case then there is no way to get at the environment variables in an unrelated process on UNIX. The nearest I have come up with is to write a debugger and attach that to the application, but even then you need to know the start address of the environment block, which is harder than it sounds. Parsing memory until you get something that looks like an environment block is possible but not reliable.

    If you are on Windows then there is a possible solution with Win32::EnvProcess, but it can be flaky depending on the version of the OS. Let the author know if you decide to take that route.

    and inherits its parent ENV variable
    Do you mean the ENV environment variable? This contains the name of the Korn shell start-up file (typically $HOME/.kshrc). Or do you mean the environment block exposed in Perl as %ENV?
Re: Getting environment variable of a process
by fullermd (Vicar) on Dec 30, 2008 at 14:24 UTC
    is there a way in perl to get the env variables generated by appln s/w may be by giving porcess ID or appln name?

    There's no portable way in *nix period to get the environment of a process other than yourself.

    A glance shows some things on CPAN like Shell::GetEnv that may let you run subprocesses and get their info, but I don't know how well that would work with long-running processes, and doesn't help with non-descendant processes anyway.

    edit: markup fix

Re: Getting environment variable of a process
by linuxer (Curate) on Dec 30, 2008 at 14:17 UTC
Re: Getting environment variable of a process
by Perlbotics (Archbishop) on Dec 30, 2008 at 18:38 UTC

    FWIW, RHEL 5 should provide the /proc file system. Since you have the PID and (presumably) sufficient permissions, you could try to read the environment from /proc/PID/environ.

    From man 5 proc:

    /proc/[pid]/environ
    This file contains the environment for the process. The entries are separated by null bytes ('\0'), and
    there may be a null byte at the end. Thus, to print out the environment of process 1, you would do:
    $ (cat /proc/1/environ; echo) | tr '\000' '\n'
    Sorry, no suggestion for the Windows XP part.

    Update: It's the environment as seen when the process was invoked. Changes applied to the environment are not propagated - at least not when I tested it under SuSE 11.1, kernel 2.6.27.7.