Why won't this example from 'Learning Perl 6th Edition' run?
I am stuck on chapter 2 exer开发者_运维知识库cise 2 page 42 of Learning Perl 6th Edition. I copied the code example for the problem from page 296. I am using Perl version 5.10.1 on Ubuntu 11.04. I get errors that I cannot figure out could someone please help? I will list the code and the error message below.
#!/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";
The error I get is:
./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>);'
You are executing the Perl script using your shell instead of perl
. Based on the fact that the line numbers are off by one, I suspect the cause of the problem is a blank line before the shebang (#!
) line. #!
must be the first two bytes of the file. Delete this blank line.
If that's not the problem, then perhaps you executed your script using
. ex2-2
or
sh ex2-2
when you should have used
perl ex2-2
or
ex2-2 # if "." is in your $PATH
or
./ex2-2
The last two requires that you make the script executable (chmod u+x ex2-2
).
It would help if you copied and pasted exactly what you executed. Notice that the line numbers are different in the example below:
$ cat x.pl
#!/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";
$ sh x.pl
x.pl: line 2: =: command not found
x.pl: line 3: print: command not found
x.pl: line 4: syntax error near unexpected token `$radius'
x.pl: line 4: `chomp($radius = <STDIN>);'
$
This was with Bash 3.x on MacOS X 10.7.1.
Given that output, I can confidently diagnose that your script was run as a shell script and not as a Perl script; bash
was used to run it.
精彩评论