-2

The issue

I found out how to use windows environment variables (e.g., %AppData%, %HomePath%, %SystemRoot%, etc.) in this SO post:

Getting the path of %AppData% in perl script

Here is the code snippet that was chosen as a working, correct answer:

 #!/usr/bin/perl

 use warnings;
 use strict;

 my $localConfPath = $ENV{localappdata};
 my $appdata = $ENV{appdata}; 

 print $localConfPath;  #will print the app path - C:\users\xxx\AppData\local
 print $appdata; #prints - C:\users\xxx\AppData\Roaming

However, this is not working on my machine in my code for some reason. My scripts work without the shebang (#!) line so I tried the script both with and without it, to no avail.

My set-up

I'm using the Perl that comes with GitBash, if that makes a difference.

What I've tried

I tried a simple Perl command line execution:

perl -e 'print %ENV{AppData}';

This didn't work. I also tried the following alternatives:

perl -e 'print %ENV{APPDATA}';
perl -e 'print %ENV{appdata}';

That also didn't work. Here's the error I get (the same for all 3 versions):

syntax error at -e line 1, near "%ENV{AppData"
Execution of -e aborted due to compilation errors.

I even tried to use the code from the SO post I mentioned in it's own file. That code doesn't work either. With the code from the post I get this error:

$ perl /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl
Use of uninitialized value in print at /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl line 7.
Use of uninitialized value in print at /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl line 8.

The lines in question then are these:

print $localConfPath;  #will print the app path - C:\users\xxx\AppData\local
print $appdata; #prints - C:\users\xxx\AppData\Roaming

I don't see why they shouldn't work.

I've checked Perl Monks, Perl Maven, Stack Overflow, and other popular Perl resources, to no avail. Even Active State did not have the answer.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Note that it's `$ENV{...}`, not `%ENV{...}` when trying to access individual items in a hash. – stevieb Feb 13 '17 at 19:47
  • Also, for one-liners on Windows, you typically need to use double-quotes: `perl -E "..."`. I've never used git bash before, so this may not apply there. – stevieb Feb 13 '17 at 19:48
  • Doh! Thanks @stevieb ... That was the issue. Everything else was fine. `$ perl -e 'print $ENV{APPDATA}';` works perfectly. – Eric Hepperle - CodeSlayer2010 Feb 13 '17 at 19:51
  • I've made it an answer since it did resolve the issue. – stevieb Feb 13 '17 at 20:03
  • @stevieb, It depends on the shell, not the Perl. If he's using `bash`, then single quotes were appropriate. – ikegami Feb 13 '17 at 20:26
  • @ikegami Thanks. I'm aware it's the shell, but never have used git bash before, I wasn't sure if it was a true bash shell, or some trickery still wrapped in the Windows `cmd.exe` shell or not. – stevieb Feb 13 '17 at 20:28
  • It wouldn't be bash if it didn't accept bash commands. – ikegami Feb 13 '17 at 20:31

1 Answers1

2

When you access individual items of a hash, you need to use the scalar sigil, $ as opposed to the hash sigil, %:

perl -e 'print $ENV{APPDATA}'
stevieb
  • 9,065
  • 3
  • 26
  • 36
  • Also, for those seeing this answer marked correctly, another thing to be mindful of is that Windows uses ALL-CAPS and is case-sensitive (for the hash key). – Eric Hepperle - CodeSlayer2010 Feb 13 '17 at 20:31
  • 1
    @EricHepperle-CodeSlayer2010 just did some testing. In `cmd.exe`, env vars are not case-sensitive. `appdata` works just fine, as does `programfiles` (for `ProgramFiles`). It's likely the bash shell forcing things to remain sane by using proper case (I can't confirm that though). – stevieb Feb 13 '17 at 20:35
  • Thanks @stevieb, I'll look into that. – Eric Hepperle - CodeSlayer2010 Mar 17 '17 at 17:48