在Windows上使用Perl,如何确保在chdir之后的正确情况下获取路径?

Cra*_*aig 4 windows perl path case-sensitive cwd

请考虑以下代码:

print cwd . "\n";
$str= "../source"; # note the lower case 's'    
chdir($str);
print cwd . "\n";
Run Code Online (Sandbox Code Playgroud)

如果我当前的目录是c:\parentdir\Source(注意大写'S'),那么它的输出将是:

c:/parentdir/Source
c:/parentdir/source

这导致我的子程序中的问题关注文件夹名称的正确情况.$ str传入我的子程序,所以我不能提前知道它是否具有正确的大小写.如何确定匹配路径的大小写正确的名称$str

更多细节在这里:

  • 我意识到这../source是一个病态的例子,但它有助于说明问题.即使$str请求当前文件夹以外的文件夹也会发生.
  • 我尝试了很多选项,包括rel2abs一个glob搜索 $str和其他选项,但它们似乎都返回" source"而不是" Source".
  • 我可以搜索$str/..所有目录,将它们全部转换为绝对路径并将它们与绝对路径版本进行比较$str,但这看起来像是一个黑客.我希望有更优雅的东西.

Sin*_*nür 10

#!/usr/bin/perl

use warnings; use strict;
use Cwd;
use File::Spec::Functions qw( canonpath );
use Win32;

print canonpath( cwd ), "\n";

chdir '../source';

print canonpath( cwd ), "\n";

print canonpath( Win32::GetLongPathName( cwd ) ), "\n";
Run Code Online (Sandbox Code Playgroud)
C:\DOCUME~1\...\LOCALS~1\Temp\t\Source> t
C:\DOCUME~1\...\LOCALS~1\Temp\t\Source
C:\DOCUME~1\...\LOCALS~1\Temp\t\source
C:\Documents and Settings\...\Local Settings\Temp\t\Source