Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Windows上使用Perl,如何确保在chdir之后获得正确的路径?_Windows_Perl_Path_Case Sensitive_Cwd - Fatal编程技术网

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

在Windows上使用Perl,如何确保在chdir之后获得正确的路径?,windows,perl,path,case-sensitive,cwd,Windows,Perl,Path,Case Sensitive,Cwd,考虑以下代码: print cwd . "\n"; $str= "../source"; # note the lower case 's' chdir($str); print cwd . "\n"; 如果我的当前目录是c:\parentdir\Source(注意大写字母“S”),则此目录的输出将是: c:/parentdir/Source c:/parentdir/source c:/parentdir/源 c:/parentdir/源 这会导致我的一个子例程出现问题,它关心文件夹

考虑以下代码:

print cwd . "\n";
$str= "../source"; # note the lower case 's'    
chdir($str);
print cwd . "\n";
如果我的当前目录是
c:\parentdir\Source
(注意大写字母“S”),则此目录的输出将是:

c:/parentdir/Source c:/parentdir/source c:/parentdir/源 c:/parentdir/源 这会导致我的一个子例程出现问题,它关心文件夹名称的正确大小写$str被传递到我的子例程中,所以我无法提前知道它的大小写是否正确。如何确定与
$str
匹配的路径的大小写正确的名称

详情如下:

  • 我意识到
    。/source
    是一个病态的例子,但它有助于 举例说明问题。即使
    $str
    请求 当前文件夹以外的文件夹
  • 我尝试了很多选择,包括
    rel2abs
    ,一种基于
    $str
    等,但它们似乎都返回“
    source
    ”而不是 “
    Source
  • 我可以搜索
    $str/。
    所有目录,将它们全部转换为 绝对路径,并将其与绝对路径版本的
    $str
    进行比较, 但这似乎是一个黑客。我希望有更优雅的东西
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我使用cygwin和svn,当svn因为区分大小写而找不到文件时,我发现了这个问题

e、 g。 $cd/cygdrive/c/svn/delphi2010/lib/checksumᜯ不是真的情况

$svn ls svn:警告:W155010:未找到节点“/cygdrive/c/svn/delphi2010/lib/checksum”。 svn:E200009:无法列出所有目标,因为某些目标不存在

$eval cd$(cygpath$(perl-le'use Win32;$d=
cygpath-aw.
;chomp($d);print Win32::GetLongPathName($d))

$pwd /cygdrive/c/svn/Delphi2010/lib/CheckSum#注意案例已恢复


svn ls又开始工作了!非常感谢。在我看来,我甚至不需要canonpath。如果我发出以下命令:$str=Win32::GetLongPathName($str);,即使$str指定了一个相对路径,
canonpath
只是在路径中使用Windows样式的斜杠,以防出现问题。明白了。这正是我要找的。再次感谢你,没有错。通常Windows文件系统都是case-instive,除非NTFS配置为其他形式。绝对正确。不幸的是,我的子程序需要正确的大小写,原因不仅仅是操作文件系统。因此,虽然Windows和Perl不在乎,但不幸的是我不得不这么做。不区分大小写,是的,但它也保留大小写。如果文件创建为
Foo
,则可以使用
Foo
访问该文件,但系统知道其名称为
Foo
#!/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";
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