Computer Hope

Software => Computer programming => Topic started by: acidblue on July 29, 2010, 10:25:27 PM

Title: Perl help with chdir in WinXp
Post by: acidblue on July 29, 2010, 10:25:27 PM
In C,  you can use '%usrename%' when changing directories.
Like this:
Code: [Select]
$path = "C:\\documents\\%username%\\dir1\\dir2";
Does Perl have something like that??
I want to be able to change directories with out having to type code for every user name.
Title: Re: Perl help with chdir in WinXp
Post by: BC_Programmer on July 29, 2010, 10:40:16 PM
use the $ENV hashtable...

Code: [Select]
$path = "C:\\documents\\".$ENV{USERNAME}."\\dir1\\dir2";


or, you could use USERPROFILE instead, which will work more readily across windows versions.



Code: [Select]
$path= $ENV{USERPROFILE}."\\dir1\\dir2";

Or, you could make a more generic cross platform solution by using FILE::HOMEDIR, and dump environment variables altogether...

Code: [Select]
use File::HomeDir;
$path=home()."\\dir1\\dir2";

Title: Re: Perl help with chdir in WinXp
Post by: acidblue on July 29, 2010, 11:53:11 PM
use the $ENV hashtable...

Code: [Select]
$path = "C:\\documents\\".$ENV{USERNAME}."\\dir1\\dir2";


or, you could use USERPROFILE instead, which will work more readily across windows versions.



Code: [Select]
$path= $ENV{USERPROFILE}."\\dir1\\dir2";

Or, you could make a more generic cross platform solution by using FILE::HOMEDIR, and dump environment variables altogether...

Code: [Select]
use File::HomeDir;
$path=home()."\\dir1\\dir2";



Thanks  BC_, thats just what I needed.  ;D

I really like the last one:
Code: [Select]
use File::HomeDir;


$path=home()."\\dir1\\dir2";