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

OK, I've got a bit of code that I don't understand what it's doing. I'm trying to find Environment temp. Now, on different computers they may or may not have some environment variables assigned so I wanted to cover my bases. I created this code:
$TempPath = $ENV{TMP} | $ENV{TEMP} | "$ENV{SYSTEMROOT}.\TEMP";
Unfortunately, all it returns is rubbish in this case:
$TempPath = C:\WOO_]o~u]STEVE~1.JON\LOCALS~1\Temp
When in reality $ENV{TMP} = C:\DOCUME~1\STEVE~1.JON\LOCALS~1\Temp
Can anyone explain to me how my first subdirectory got FUBARed?

Replies are listed 'Best First'.
Re: Best way to find Environment TEMP
by FunkyMonk (Bishop) on Jun 11, 2007 at 19:36 UTC
    $TempPath = $ENV{TMP} | $ENV{TEMP} | "$ENV{SYSTEMROOT}.\TEMP";
    You want logical-or ("||") not bitwise-or ("|").
    $TempPath = $ENV{TMP} || $ENV{TEMP} || "$ENV{SYSTEMROOT}.\TEMP";

    See perlop for the reasons why.

      Thank you, that was it.
Re: Best way to find Environment TEMP
by tinita (Parson) on Jun 11, 2007 at 21:18 UTC
      have you considered File::Temp?

      Quite a precious module but aimed at a task that's likely to be orthogonal to the OP's. In fact he may even want to use his $TempPath in conjunction with F::T to create temporary files there. Of course File::Spec's tmpdir() gets closer to the original task and F::T uses it behind the curtains for reasonable defaults.