Subject: | Dynamic computation may go wrong for relative paths |
Consider the following example module, saved as Dirselftest.pm:
# -*- perl -*-
package Dirselftest;
use strict;
use Dir::Self;
sub test { warn __DIR__ }
sub test2 { chdir "/" or die $!; warn __DIR__ }
1;
__END__
And then use it like this:
$ perl5.12.4 -I. -MDirselftest -e 'Dirselftest::test();
Dirselftest::test2()'
/tmp at Dirselftest.pm line 18.
/ at Dirselftest.pm line 19.
The dynamic computation of __DIR__ is obviously problematic in the
presence of relative directories in the @INC and a chdir() call
somewhere in the program. Using ':static' fixes things here. As I think
that the use case of having the same package in different files is quite
unusual, but having relative paths in @INC is not so unusual, I wonder
whether the default of dynamic vs. constant __DIR__ should be swapped.
But in any case, I think such a case should at least be documented as a
possible caveat.
Regards,
Slaven