[英]What Perl module(s) do I use to obtain an absolute path (including filename) from a relative one on Windows?
I can only imagine I'm not searching correctly; 我只能想象我搜索不正确; this seems like an obvious question to be asked here.
这似乎是一个显而易见的问题。 My apologies if this is a duplicate.
如果这是重复的话,我深表歉意。
I'm writing a Perl program that will take a filename as a command-line argument. 我正在编写一个Perl程序,它将文件名作为命令行参数。 I need to convert the filename (or the filename with a relative path attached) to an absolute path (specifically to work with Win32::OLE).
我需要将文件名(或带有相对路径的文件名)转换为绝对路径(特别是与Win32 :: OLE一起使用)。
I tried using Cwd 's 'abs_path', and that almost does what I want, but it returns it using a Unix-style path instead of a Win32 one. 我尝试使用Cwd的'abs_path',几乎可以实现我想要的功能,但是它使用Unix风格的路径而不是Win32路径返回它。
Is there a module that will convert the path, or perhaps a better module to use in the first place? 是否有将转换路径的模块,或者也许是首先使用的更好的模块?
I use rel2abs
from File::Spec . 我使用File :: Spec中的
rel2abs
。 You have to be careful though: that might call getdcwd
from Cwd
, and it will assume that you want the current working directory for the current drive. 但是,您必须要小心:这可能会从
Cwd
调用getdcwd
,并且将假定您需要当前驱动器的当前工作目录。 If the file is on some other drive, you'll have to fix that up yourself or supply the second argument to set the base path. 如果文件在其他驱动器上,则必须自己进行修复或提供第二个参数来设置基本路径。
use File::Spec::Functions qw(rel2abs);
print rel2abs($ARGV[0]), "\n";
my($foo) = abs_path($some_file);
$foo =~ s{/}{\\}g;
print "FOO: $foo\n";
I use Cwd's abs_path and then use a regex to convert the slashes when I really need it done. 我使用Cwd的abs_path,然后在真正需要使用正则表达式时将其转换为斜杠。 But I've found that for most uses, Unix-style slashes work just fine.
但是我发现对于大多数用途,Unix样式的斜杠都可以正常工作。 It's only for the occasional "pass a filename to that annoyingly limited program" that I end up needing to convert the slashes.
我只是偶尔需要“将文件名传递给该烦人的受限程序”,最终我需要转换斜杠。
use Cwd 'abs_path';
my $path = abs_path($rel_path);
# and only if necessary...
$path =~ s'[/\\]+'\\'g; # use Windows-style slashes
$path =~ s'^\\'\\\\'; # handle network path
But then.. I use a lot of network paths, with or without a mapped drive reference. 但是,然后..我使用了很多网络路径,无论有没有映射的驱动器参考。 Your mileage may vary.
你的旅费可能会改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.