windows - Perl 中的“打开”语句拆分包含空格的路径 - 如何读取路径中包含空格的文件?

48 小时以来,我一直在用头撞同一堵墙。如何将命令行参数传递给 Perl,以便当 Perl 有机会打开文件时,它可以正确处理出现在 a) 目录或 b) 文件名中的 Windows 样式空格:

# open( PRELIM, "\"$ifile\"") or die "Cannot open $ifile";
# open( PRELIM, '\"$ifile\"') or die "Cannot open $ifile";
## Both these lines cannot deal with a space present in the path:
# open PRELIM, $ifile or print "\n* Couldn't open ${ifile}\n\n" && return;
# $ifile = qq($ifile);  Doesn't help, still leaves the file as if it was 'two' files
#
  # Quotes around $ifile do no good either
open( PRELIM, $ifile) or die "Cannot open $ifile"

通常,在变量“$ifile”中出现空格之前,上面的行是正确的。

最佳答案

要将命令行参数作为还包含空格的单个参数传递,请将其括在引号内。例如:

perl script-name.pl "C:\My path with spaces"

这将导致参数作为单个单词处理。

(在你的代码中,如果你硬编码反斜杠,那么你需要将它们加倍,因为“\”是一个特殊的元字符,因此“\”将在编译时转换为“\” -时间。)

https://stackoverflow.com/questions/5246402/

相关文章:

ms-access - 通过 Access VBA 代码保存查询

wordpress - 如何检索当前获取附件的 MIME 类型

sql - 分段获取 SQL 结果

vb6 - 如何枚举计算机上可用的 COM 端口?

sql - 在 SQL 中将列的子集重置为其默认值?

php - 在 Web 服务器环境中的 PHP 中启用进程控制 (PCNTL) 有哪些副作用?

database-design - 将单一类型关联到多个类型

android - 从程序中启动隐藏的 Android 设置 Activity

ibm-midrange - 有没有人使用过 IBM Net.Data

php - 使用 preg_replace 删除特定字符周围的空格