Using System to read mixed cased environment variables on Windows

Discussion in 'Perl Misc' started by Thierry, Jun 27, 2008.

  1. Thierry

    Thierry Guest

    On Windows XP Pro 32 bit, if I want to output environment variables
    PYTHON or ProgramFiles, I use the set command which output the
    following:

    C:\set PYTHON
    PYTHON=C:\Python24\python.exe
    C:\set ProgramFiles
    ProgramFiles=C:\Program Files

    If I used Perl 5.003_07 and use the system subroutine to show the
    environment variables, I get the following. Perl script is:

    system("set PYTHON");
    system("set ProgramFiles");

    Output is:
    PYTHON=C:\Python24\python.exe
    PROGRAMFILES=C:\Program Files

    You'll notice that through system, the environment variable
    ProgramFiles is all in upper case. Is there a way to preserve the
    mixed case of the environment variable through system(...)?
    Thierry, Jun 27, 2008
    #1
    1. Advertising

  2. Thierry

    Ron Bergin Guest

    On Jun 27, 10:00 am, Thierry <> wrote:
    > On Windows XP Pro 32 bit, if I want to output environment variables
    > PYTHON or ProgramFiles, I use the set command which output the
    > following:
    >
    > C:\set PYTHON
    > PYTHON=C:\Python24\python.exe
    > C:\set ProgramFiles
    > ProgramFiles=C:\Program Files
    >
    > If I used Perl 5.003_07


    That's a fairly old version. You should upgrade to at least 5.8

    > and use the system subroutine to show the
    > environment variables, I get the following. Perl script is:
    >
    > system("set PYTHON");
    > system("set ProgramFiles");
    >

    There's no reason to use a system call. Your environment variables
    are already stored in the %ENV hash.

    print $ENV{'ProgramFiles'};
    Ron Bergin, Jun 27, 2008
    #2
    1. Advertising

  3. Thierry

    sisyphus Guest

    On Jun 28, 4:00 am, Thierry <> wrote:
    > On Windows XP Pro 32 bit, if I want to output environment variables
    > PYTHON or ProgramFiles, I use the set command which output the
    > following:
    >
    > C:\set PYTHON
    > PYTHON=C:\Python24\python.exe
    > C:\set ProgramFiles
    > ProgramFiles=C:\Program Files
    >
    > If I used Perl 5.003_07 and use the system subroutine to show the
    > environment variables, I get the following.  Perl script is:
    >
    > system("set PYTHON");
    > system("set ProgramFiles");
    >
    > Output is:
    > PYTHON=C:\Python24\python.exe
    > PROGRAMFILES=C:\Program Files
    >
    > You'll notice that through system, the environment variable
    > ProgramFiles is all in upper case.  Is there a way to preserve the
    > mixed case of the environment variable through system(...)?


    I find that case is preserved with perl 5.6.2, 5.8.8 and 5.10.0:

    C:\>set MyTest=C:\MyTest

    C:\>set MyTest
    MyTest=C:\MyTest

    C:\CVS>perl -e "system(\"set MyTest\")"
    MyTest=C:\MyTest

    C:\CVS>set MITEST=C:\MiTest

    C:\CVS>perl -e "system(\"set MiTest\")"
    MITEST=C:\MiTest

    Not quite sure why you get the behaviour you reported ... perhaps it
    is just that antiquated version of perl you're running. I guess you
    could update your perl and see if the behaviour changes. (Though as
    Ron Bergin has already indicated, there are better ways of accessing
    the environment variables anyway.)

    Cheers,
    Rob
    sisyphus, Jun 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shimon Sim

    Switching to ASP.NET 2 cased exception

    Shimon Sim, May 1, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    4,305
    Gman22
    Oct 13, 2010
  2. Jim Potts
    Replies:
    3
    Views:
    361
    Felix McAllister
    Oct 3, 2003
  3. Henaro
    Replies:
    5
    Views:
    379
    red floyd
    May 7, 2007
  4. Replies:
    6
    Views:
    260
    Steve Callaway
    Jul 19, 2005
  5. Roger Pack

    special cased?

    Roger Pack, Dec 19, 2009, in forum: Ruby
    Replies:
    8
    Views:
    130
    David A. Black
    Dec 22, 2009
Loading...

Share This Page