in reply to Test to see if directories are the same

if you have Cwd on win32 than this should work:
sub sameDirectory { my ($s,$d) = @_; -d $s and $s = abs_path($s) or return -1 ; -d $d and $d = abs_path($d) or return -1 ; $s eq $d ? 1 : 0 ; }

--
AltBlue.

Replies are listed 'Best First'.
(tye)Re: Test to see if directories are the same
by tye (Sage) on Jan 18, 2001 at 00:36 UTC

    Note that this doesn't work in the face of symbolic links (or in the face of file systems that support hard links to directories better than most Unix file systems do).

    Perhaps someone should combine the two sets of tests into one that works lots of places. I'd personally prefer testing for 0 == (stat($d))[1] over trying to hard-code some list of $^O values (or are there file systems where i-node 0 might be a valid directory?).

            - tye (but my friends call me "Tye")
      no idea if it'd be working against hardlinks (don't any OS that can do that :( ), but it sure does with symlinks :) .. it's written in Cwd docs:
      The abs_path() function takes a single argument and returns the absolute pathname for that argument. It uses the same algorithm as getcwd(). (Actually, getcwd() is abs_path(".")) Symbolic links and relative-path components ("." and "..") are resolved to return the canonical pathname, just like realpath(3). Also callable as realpath().

      ... ofc, cause you mentioned it i've just tested it and seems to be working ok on linux and sunos.

      --
      AltBlue.

      I did some limited tests and did note that on RedHat 6.2 I got positive results with symbolic links. I just did a quick check on NT with the base test.
      Cwd.pm does run it's own $^O checks, so it may be workable on most cases.

        Ah, I guess symbolic links can usually be untangled. It seems there are weird directory structures under Unix that can't reliably be untangled but I don't recall the details (hard links to directories is certainly one but doing those under Unix usually has such bad consequences from fsck that you don't run into it).

                - tye (but my friends call me "Tye")
Re: Re: Test to see if directories are the same
by merlyn (Sage) on Jan 18, 2001 at 02:12 UTC
    -d $s and $s = abs_path($s) or return -1 ; -d $d and $d = abs_path($d) or return -1 ;
    Don't write the same code twice. Ever. :)
    -d $_ and $_ = abs_path($_) or return -1 for $s, $d;
    Except that's using "foo AND bar OR bletch" where it should be using "foo ? bar : bletch", so I'd tighten that up to:
    (-d $_) ? ($_ = abs_path($_)) : (return -1) for $s, $d;

    -- Randal L. Schwartz, Perl hacker

      merlyn: Don't write the same code twice. Ever. :)
      oops. sorry :))

      this gave me an idea: why not give ppl a way to compare N directories? :) here is the test code:

      #!/usr/bin/perl -w use strict; use Cwd 'abs_path'; defined @ARGV and print sameDirectory(@ARGV),$/; sub sameDirectory { (-d) ? $_ = abs_path $_ : return -1 for @_; $_[0] ne $_ and return 0 for @_; 1; }

      --
      AltBlue.