Hello.
Hello!
I write a perl script that 5 different people will use.
Each one of the 5 will run the script on the same computer, but will login to this computer with a different user_name and so (I guess) with a different user environment variable.
If we were working on Unix, I could simply use the WHOAMI unix command in the perl script. But we are working with WINDOWS.
So, in short, how can the perl script know who is the current user, without asking him, in order to use a different directlry for each user ?
Ok, first.... perldoc is your friend. Reference it often and always. ( just a tip that could have helped here).
That said, take a look at
this page. That is the perldoc page for Perl's special variables. On that page, you will see that Perl has the hash %ENV already defined, containing all of the environment variables as keys and their values as.....well.....the values.
Now, the question would be, "How do I utilize that variable?". That question can be answered by visiting
this perldoc page. That page is for the keys function, which basically grabs all of the keys in a hash for you.
The code on that page(the very first example) is the code that you are looking for. Here it is:
-
@keys = keys %ENV;
-
@values = values %ENV;
-
while (@keys) {
-
print pop(@keys), '=', pop(@values), "\n";
-
}
-
Now, if you are using strict and warnings (as you should ALWAYS be doing), then you will want to prepend the array definitions with "my" or Perl will throw complaints at you.
When you run this code on your Windows machine (perl <scriptName>), it will print out ALL of the environment variables defined, and their corresponding values (using the values function shown above). One of the variables is "USERNAME".
Now, all you have to do is write some code that will reference the USERNAME variable from the hash and grab its value so your script can use it.
Hope this helps to put you on the path to completing your script.
Regards,
Jeff