Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 这个perl代码有什么问题?_Windows_Perl_System - Fatal编程技术网

Windows 这个perl代码有什么问题?

Windows 这个perl代码有什么问题?,windows,perl,system,Windows,Perl,System,我正在尝试使用perl在系统命令提示符中运行一组命令 这是密码 #!/usr/local/bin/perl -w use strict; print_prompt(); sub print_prompt { print "What's your name?"; system("G:\"); system("cd Documents and Settings/Administrator/eworkspace/Sample"); print `ant`; } 但这让我犯了以下错误

我正在尝试使用perl在系统命令提示符中运行一组命令

这是密码

#!/usr/local/bin/perl -w

use strict;

print_prompt();


sub print_prompt {
print "What's your name?";
  system("G:\");
system("cd Documents and Settings/Administrator/eworkspace/Sample");
  print `ant`;

}
但这让我犯了以下错误

Bareword found where operator expected at execute.pl line 11, near "system("cd"
 (Might be a runaway multi-line "" string starting on line 10)
String found where operator expected at execute.pl line 11, at end of line
    (Missing semicolon on previous line?)
syntax error at execute.pl line 11, near "system("cd Documents "
Can't find string terminator '"' anywhere before EOF at execute.pl line 11.
我如何解决这个问题?此代码中可能有什么错误?我是否需要为空格指明位置?

问题在于:

system("G:\");
  • 这不是一个明智的命令
  • 反斜杠转义的是
    ,因此字符串实际上是

    "G:\");
    system("
    
    qq{G:”;\n系统(}
    ,带有备用分隔符

    字符串后面必须有某种形式的运算符,但
    cd
    不是运算符

  • 解决方案:永远不要使用反斜杠作为路径分隔符,它们只会导致问题。删除奇怪的
    G:\
    命令,它到底应该做什么

    要在字符串中包含文字反斜杠,必须对其进行转义:
    \\

    这两行:

    system("G:\");
    system("cd Documents and Settings/Administrator/eworkspace/Sample");
    
    有两种方式被破坏。首先,最上面的一个被破坏,就像其他人在我之前描述的那样。
    \
    转义
    ,这样它就不会关闭引用的字符串,文件其余部分的语法也会被破坏

    但是,第二,这两行在更深层次上都被打断了。它们不做你认为的事情。实际上,它们都不做任何事情。
    system
    命令调用一个新的shell环境来运行命令。新环境从父环境(运行你的代码的环境)继承值。这些值包括当前目录。然后,您可以在新的子环境中更改当前目录。但是,当
    system
    命令完成(立即发生)时,您的新环境将被破坏。您的程序将使用原始的当前目录继续在原始环境中运行


    您可能应该查看Perl的内置函数。

    来吧。只需查看错误消息:可能有一个“失控的多行
    -字符串从第10行开始。查看第10行的字符串。思考为什么它可以跨多行。甚至您文章的语法突出显示也在告诉您!您应该参考。