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

Fellow Monks, The question i have to ask is regarding chdir. Apparently, if you call chdir without an argument, perl best tries to determine your home directory and changes the path to it. So what i tried to do was this
if(/$/){ chdir or die "Unable to locate home"; }
the die message always got thrown. This is for using on a windows comp. Am i calling chdir the wrong way?

Replies are listed 'Best First'.
Re: Changing path to home directory
by AppleFritter (Vicar) on Mar 04, 2015 at 23:10 UTC

    From chdir:

    If EXPR is omitted, changes to the directory specified by $ENV{HOME}, if set; if not, changes to the directory specified by $ENV{LOGDIR}. [...] If neither is set, chdir does nothing. It returns true on success, false otherwise. See the example under die.

    So my (entirely uneducated) understanding is that

    1. On Windows, neither of the environment variables is likely to be set;
    2. Therefore, chdir without an argument does in fact do nothing;
    3. Therefore, chdir returns false;
    4. Therefore, you die.

    As usual, I don't claim to actually know much if anything about Perl (I'm but a pony, after all!), and others will be able to provide more detailed answers.

      If neither is set, chdir does nothing. It returns true on success, false otherwise;

      So .... if it's supposed to do nothing, and successfully does nothing, shouldn't it then return true ?
      Seems to me AppleFritter has correctly identified that, under those conditions, chdir() returns false - but it's not spelled out very clearly in the documentation.

      I think the documentation would be better written as:
      "If neither is set, chdir does nothing and returns false. Otherwise it returns true on success, or false on failure"


      Cheers,
      Rob
        The "Otherwise it returns true on success, or false on failure." is implied or at least you could write that about any function. What the documentation should indicate is when failure(s) can be encountered. In this case I think what needs to be indicated is the return code is translated(y/01/10/) and passed from the underlying system call.
        When a statement is always true, except for when it's false, the cases when the statement is false _need_ to be documented. -- Mike Mestnik
Re: Changing path to home directory
by Anonymous Monk on Mar 04, 2015 at 23:50 UTC

    I don't have a Windows machine at the ready, but File::HomeDir should work on various platforms.

    use File::HomeDir (); my $HOME = File::HomeDir->my_home; chdir $HOME or die "Couldn't chdir $HOME: $!";

      Close!

      #!/usr/bin/perl use 5.018; use strict; use warnings; # 1118814 use Cwd; use File::HomeDir(); my $cwd = getcwd(); say "At Ln 12: \$cwd: $cwd"; my $HOME = File::HomeDir->my_home; chdir $HOME or die "Couldn't chdir $HOME: $!"; say "At Ln 18; \$HOME: $HOME"; $cwd = getcwd(); say "At Ln 21, \$cwd: $cwd"; =head Execution: C:\>D:\PMonks\1118814.pl At Ln 12: $cwd: C:/ At Ln 18; $HOME: C:\Users\ww At Ln 21, $cwd: C:/Users/ww =cut
        Only a remark: on windows be very carefull mixing the Perl's idea of cwd with the OS's one.
        Given you have a drive y: can you predicdt the output of the following command? ;=)
        c:\path> y: & cd c:\ & dir c: & cd c:\Windows & dir c: & dir \
        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Good to know it works, thanks for testing. In regards to the "Close!" I don't see the significant difference? (AFAIK backslashes are "more correct" on Windows since forward slashes usually but don't always work?)