develooper Front page | perl.perl5.porters | Postings from May 2004

Re: Cwd::abs_path bug?

Thread Next
From:
Ken Williams
Date:
May 30, 2004 14:54
Subject:
Re: Cwd::abs_path bug?
Message ID:
AB4A33EC-B247-11D8-87F5-000A95BD9874@mathforum.org
Hi Marco,

I think I'd call this a bug - if the file exists and isn't a directory, 
then we should probably just do the obvious thing and return its 
absolute path anyway.  Also, since it's been working on some platforms, 
people might be depending on it.

How about the following patch to Cwd.pm (untested - I only have OS X to 
test on at the moment)?

-----------------------------------------
@@ -374,6 +374,16 @@
         _carp("stat($start): $!");
         return '';
      }
+
+    unless (-d _) {
+       # Make sure we can be invoked on plain files, not just 
directories.
+       # NOTE that this routine assumes that '/' works as a directory 
separator
+       my ($dir, $file) = $start =~ m{^(.*)/(.+)$}
+           or return cwd() . '/' . $start;
+
+       return abs_path($dir) . '/' . $file;
+    }
+
      $cwd = '';
      $dotdots = $start;
      do
-----------------------------------------

  -Ken

On May 29, 2004, at 11:22 AM, Marco Moreno wrote:

> Ken,
>
> I'm a bit confused as to the use of Cwd::abs_path.  The docs state:
>
>        abs_path
>              my $abs_path = abs_path($file);
>
>            Uses the same algorithm as getcwd().  Symbolic links and 
> relative-
>            path components ("." and "..") are resolved to return the 
> canonical
>            pathname, just like realpath(3).
>
> This seems to imply that abs_path may be used on a plain file as well 
> as a directory.  While this seems to work on some systems such as 
> MacOS X (confirmed) and apparently other flavors of *nix, it does not 
> work correctly on ActivePerl (win32) nor cygwin as it attempts to 
> chdir into the directory and croaks when it cannot.  I'm inclined to 
> believe that the fact that abs_path may work for plain files on some 
> systems is purely accidental and that it was never designed to do so.
>
> If this is indeed the case, then the docs are confusing (e.g. "$file" 
> instead of "$dir") and probably should state explicitly that its 
> argument must be a directory.  Perhaps abs_path could verify a 
> directory and die more gracefully.
>
> But if abs_path is intended to support plain files, then there are 
> bugs in the implementation on some platforms (those that don't call 
> xs???).
>
> Hope this helps,
>
> Marco Moreno
>


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About