为什么"学习Perl第6版"的这个例子不会运行?

Dav*_*vid 1 perl

我被困在Learning Perl第6版的第2章练习2第42页.我从第296页复制了问题的代码示例.我在Ubuntu 11.04上使用Perl版本5.10.1.我得到的错误,我无法弄清楚有人可以帮忙吗?我将列出下面的代码和错误消息.

#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circ = 2 * $pi * $radius;
print "The circumference of a circle of radius $radius is $circ.\n";
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

./ex2-2: line 3: =: command not found
Warning: unknown mime-type for "What is the radius? " -- using "application/octet-stream"
Error: no such file "What is the radius? "
./ex2-2: line 5: syntax error near unexpected token `$radius'
./ex2-2: line 5: `chomp($radius = <STDIN>);'
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 9

您正在使用shell而不是shell来执行Perl脚本perl.基于行号偏离1的事实,我怀疑问题的原因是shebang(#!)行之前的空白行.#!必须是文件的前两个字节.删除此空白行.

如果那不是问题,那么也许您使用了执行脚本

. ex2-2
Run Code Online (Sandbox Code Playgroud)

要么

sh ex2-2
Run Code Online (Sandbox Code Playgroud)

什么时候应该使用

perl ex2-2
Run Code Online (Sandbox Code Playgroud)

要么

ex2-2       # if "." is in your $PATH
Run Code Online (Sandbox Code Playgroud)

要么

./ex2-2
Run Code Online (Sandbox Code Playgroud)

最后两个要求您使脚本可执行(chmod u+x ex2-2).