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/