Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Perl help with chdir in WinXp  (Read 4239 times)

0 Members and 1 Guest are viewing this topic.

acidblue

    Topic Starter


    Rookie

    Perl help with chdir in WinXp
    « 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.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Perl help with chdir in WinXp
    « Reply #1 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";

    I was trying to dereference Null Pointers before it was cool.

    acidblue

      Topic Starter


      Rookie

      Re: Perl help with chdir in WinXp
      « Reply #2 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";