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

Hi,
somehow the function chdir in standard library module Cwd doesn't work as advertised or I'm doing something fundamentally wrong.

perldoc Cwd says if you override chdir with Cwd's chdir, then your PWD environment stays up-to-date. But this perl snippet:

#!/usr/local/bin/perl -w use strict; use Cwd; use Cwd 'chdir'; chdir '/x/y/z'; print getcwd(),"\n";
called from the root dir outputs just
/
instead of
/x/y/z

And yes, /x/y/z did exist.
I tried this on Solaris8 and Suse Linux 9.1 with perl versions 5.005 and 5.8.5.

Any idea?

****** UPDATE:

I shouldn't have skipped lunch. It was a simple typo in the path name.

Replies are listed 'Best First'.
Re: function chdir in Module Cwd broken?
by ikegami (Patriarch) on Nov 08, 2004 at 20:43 UTC

    Your snippet works for me in WinXP and FreeBSD. Try printing the error message:

    chdir '/x/y/z' or die("$!$/");

    Not only must the directory exist, but the user must have eXecute permissions to /x, /x/y and /x/y/z.

Re: function chdir in Module Cwd broken?
by Aighearach (Initiate) on Nov 08, 2004 at 20:39 UTC
    First of all, you shouldn't use the same module twice. You can get rid of the second one, it seems to export chdir and getcwd by default.

    Are your permissions correct for the dir? Specifically, are /x, /x/y and /x/y/z all executable by your user? There are the test results I got:

    $ perl -v This is perl, v5.8.0 built for i586-linux-thread-multi $ perl -MCwd -le 'chdir "/tmp"; print getcwd();' /tmp $ perl -MCwd -le 'chdir "/root"; print getcwd();'

    --
    Snazzy tagline here
      While I agree the duplicate use is bad in the OP, I disagree with your blanket statement that a module shouldn't be used twice. For example, my script and a module used by my script might both use FileHandle. It can even be acceptable within the same file. Consider a style I often use:

      use Some::Module (); # Don't import anything here. # ---------- package Package1; use Some::Module qw( ... ); # import something ... # ---------- package Package2; use Some::Module qw( ... ); # import something ... # ---------- package main; { ... main program ... }

      One could argue this should be split into multiple files, but you'd still be including the Some::Module more than once.

        Yeah, but I think that it's still true as a general principle. Of course there are exceptions. I'd even go so far as to say, there are exceptions to the rule "never use goto LABEL" . But I would still say, when I was looking at something other than that cosmically rare situation, that the general rule is valid.

        The only reason your example is okay, IMO, is that you're pressed for time and are planning to split that module into more than one later, when your teapot isn't boiling over or whatever. That's the only reason for it; it's temporary and prevents a problem later. Otherwise it's redundant, confusing, and reinforces and incorrect notion of scope.


        --
        Snazzy tagline here

      Actually as a style nit I'd say get rid of the first one and leave the one which explicitly states what it's importing (as opposed to the first which depends on the graces of @EXPORT). But again that's just me and is neither here nor there with respect to chdir functioning or not.

        In that case it would have to be altered to add getcwd(). But another view is that being overly explicit creates a bunch of noise. And it can make things brittle. Also there are rare cases where that changes the expected behavior of a module. The important thing is, know what use Foo "bar" means, and use it in a self-consistent way.

        --
        Snazzy tagline here
      You are right, the second use seems to be superfluous, contrary to the perldoc of chdir.

      But even if the perldoc were true, I could have used Cwd::chdir or listed all the symbols in the use statement. It would be safer, but I'm not sure if modules shouldn't be designed to be called more than once.
      Similar to header files in C, where it made header handling so much easier.

Re: function chdir in Module Cwd broken?
by borisz (Canon) on Nov 09, 2004 at 13:51 UTC
    Hi, I do not think it is broken perhaps chdir faild. Try this please:
    !/usr/local/bin/perl -w use strict; use Cwd qw/getcwd chdir/; chdir '/x/y/z' or die $!; print getcwd(),"\n";
    Boris