4

The FindBin module determines the current working directory at compile time using this function (see source code here):

sub cwd2 {
   my $cwd = getcwd();
   # getcwd might fail if it hasn't access to the current directory.
   # try harder.
   defined $cwd or $cwd = cwd();
   $cwd;
}

where cwd() and getcwd() are both imported from the Cwd module. In which cases would getcwd() fail but cwd() still work? ( I am most interested in the Linux platform if that matters )

See also:

Community
  • 1
  • 1
Håkon Hægland
  • 32,521
  • 18
  • 64
  • 139

1 Answers1

4

As the comment says, getcwd() can fail if the process doesn't have enough access to the current directory (and all its ancestors). cwd() has the potential to shell out to a possibly-setuid external pwd command and get the directory even in that case.

More generally, getcwd() is documented to behave like the POSIX getcwd call. cwd() is documented as being more flexible. The difference between them will likely be very small on Linux, but both the Cwd and FindBin modules are intended to work on all the platforms that Perl support. Those include Windows, VMS, pre-OSX MacOS, IBM z/OS and a bucketload of others. On those, the difference between the commands may well be significant.

Calle Dybedahl
  • 4,691
  • 2
  • 15
  • 21