如何在Perl中构建与OS无关的文件路径,包括可选的Windows驱动器号?

Ser*_*nik 7 windows perl path

我需要在Perl脚本中构造一个文件路径.我应该使用哪个路径分隔符来允许我的脚本在Windows和Unix上运行?

请记住,Windows需要驱动器号.

bri*_*foy 12

你想要File :: Speccatpath:

       catpath()
         Takes volume, directory and file portions and returns an entire path.
         Under Unix, $volume is ignored, and directory and file are
         concatenated.  A '/' is inserted if need be.  On other OSes, $volume
         is significant.

             $full_path = File::Spec->catpath( $volume, $directory, $file );


小智 8

你想要File :: Spec.Unix,Win32MacOS以及其他版本都有特定版本.

  • 您永远不会直接使用平台特定版本.你只需要File :: Spec.它为你找出了其余的. (6认同)

Sch*_*ern 5

如果您发现File :: Spec繁琐,请尝试Path :: Class.它为您提供了目录和文件对象,而不必在字符串上调用long winded File :: Spec类方法.