在Windows上使用Perl,如何确保在chdir之后以正确的格式获取路径? [英] Using Perl on Windows, how can I ensure I get the path in the correct case following a chdir?
本文介绍了在Windows上使用Perl,如何确保在chdir之后以正确的格式获取路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码:
print cwd . "\n";
$str= "../source"; # note the lower case 's'
chdir($str);
print cwd . "\n";
如果我当前的目录是c:\parentdir\Source
(请注意大写的"S"),则输出为:
If my current directory is c:\parentdir\Source
(note the capital 'S'), the output of this will be:
c:/parentdir/Source
c:/parentdir/source
这会在我的子程序中引起问题,该子程序关心文件夹名称的正确大小写. $ str被传递到我的子例程中,因此我无法提前知道它是否具有正确的大小写.如何确定与$str
匹配的路径的大小写正确?
This causes problems in a subroutine of mine that cares about the correct case of folder names. $str is passed in to my subroutine, so I can't know ahead of time whether it has the correct case. How do I determine the case-correct name of a path that matches $str
?
此处有更多详细信息:
- 我意识到
../source
是一个病理例子,但它可以 说明问题.即使$str
正在请求 当前文件夹以外的其他文件夹. - 我尝试了多种选择,包括
rel2abs
,这是对$str
和其他,但是它们似乎都返回"source
"而不是 "Source
". - 我可以在
$str/..
中搜索所有目录,并将它们全部转换为 绝对路径,并将它们与$str
的绝对路径版本进行比较, 但这似乎是一个hack.我希望有一些更优雅的东西.
- I realize that
../source
is a pathological example, but it serves to illustrate the problem. It occurs even if$str
is requesting a folder other than the current one. - I have tried numerous options, including
rel2abs
, a glob search on$str
, and others, but they all seem to return "source
" instead of "Source
". - I could search
$str/..
for all directories, convert them all to absolute paths and compare them to an absolute path version of$str
, but that seems like a hack. I was hoping for something more elegant.
推荐答案
#!/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
这篇关于在Windows上使用Perl,如何确保在chdir之后以正确的格式获取路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文