Perl 处理目录分隔符
我真的应该知道这一点,但我主要使用 Linux、Mac OS X 和 Windows,它们都使用正斜杠 (/
) 作为目录分隔符(Windows 可以使用 \< /code> 或
/
。)。
这意味着当我通常用 Perl 编写程序时,我可以简单地使用 /
作为目录分隔符,一切都很好。但是,我知道 File::Spec
应该允许文件分隔符的可移植性(无论这意味着什么)。
如果我所在的系统不使用正斜杠作为目录分隔符,我知道用户希望能够使用默认分隔符输入文件并查看使用默认分隔符的输出。 (例如,Windows 用户将输入并期望输出为 C:\Users\smith\Documents
而不是 C:/Users/smith/Documents
),但是什么Perl 内部做吗?
尽管平台可能使用什么作为目录分隔符,但我可以在内部处理文件时仅使用正斜杠吗?例如,我有一个目录 $dir
和一个名为 $file
的文件,我想打开该文件。我可以简单地说 $dir/file
,还是必须使用 File::Spec
为我连接名称?
事实上,Perl 程序需要目录名称中的正斜杠吗?我正在编写一个模块,并将向调用程序传递文件名。我应该将文件指定为 /foo/bar/fubar
还是如果系统像早期的 Macintosh 操作系统一样使用冒号,例如 :foo:bar:fubar
?
I really should know this, but I've worked mainly with Linux, Mac OS X and Windows which all use the forward slash (/
) as a directory separator (Windows can use either \
or /
.).
That means when I normally write programs in Perl, I can simply use /
as the directory separator and everything is fine. However, I know that File::Spec
is suppose to allow for the portability of file separators (whatever that means).
If I am on a system that does not use forward slashes as a directory separator, I understand that users expect to be able to input files with the default separators and see output with the default separators. (For example, a Windows user will input and expect output to be C:\Users\smith\Documents
and not C:/Users/smith/Documents
), but what does Perl do internally?
Can I, despite what the platform may use as a directory separator, simply use forward slashes when I'm dealing with files internally. For example, I have a directory $dir
and a file called $file
, and I want to open the file. Can I simply say $dir/file
, or do I have to use File::Spec
to concat the name for me?
In fact, do Perl programs require forward slashes in directory names? I'm writing a module, and will be delivering file names to the calling program. Should I give the file as /foo/bar/fubar
or if the system uses colons like the early Macintosh OS, say :foo:bar:fubar
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
perlport
几乎涵盖了有关此主题的所有内容。也就是说,不接受/
作为路径分隔符的系统很少见,并且您可能不会在任何地方忠实地使用File::Spec
获得太多收益。但也要小心区分目录分隔符的内部和外部用途。例如,这将在 Windows 上运行:但这可能不会,因为它需要由 Windows shell 处理:
perlport
says almost everything there is to say about this subject. That said, systems that can not accept/
as the path separator are rare, and you might not have that much to gain from usingFile::Spec
faithfully everywhere. But also be careful to distinguish internal and external uses of the directory separator. For example, this will work on Windows:but this might not, because it needs to be processed by the Windows shell: