2014-10-31 64 views
-4

我想写一个perl脚本有一个菜单,用户在列表中选择一个(1-3),然后要求用户输入一个字符串(取决于所选的菜单)这应该被读作输入并在控制台中打印。但是在执行脚本时,我发现这个错误。这里有什么可能是错的?Perl脚本返回错误全局符号需要明确的包

~] # perl sample.pl 

Global symbol "$user" requires explicit package name at ./sample.pl line 26. 
Global symbol "$user" requires explicit package name at ./sample.pl line 27. 
Global symbol "$user" requires explicit package name at ./sample.pl line 28. 
Global symbol "$process" requires explicit package name at ./sample.pl line 37. 
Global symbol "$process" requires explicit package name at ./sample.pl line 38. 
Global symbol "$process" requires explicit package name at ./sample.pl line 39. 
Missing right curly or square bracket at ./sample.pl line 52, at end of line 
syntax error at ./sample.pl line 52, at EOF 
Execution of ./sample.pl aborted due to compilation errors. 

#!/usr/bin/perl 

use strict; 
use warnings; 
use Switch; 

my $input = ''; 

while ($input ne '3') 
{ 
clear_screen(); 

print "1. user\n". 
     "2. process\n". 
     "3. exit\n"; 

print "Enter choice: "; 
$input = <STDIN>; 
chomp($input); 

{ 
    case '1' 
{ 

print "Enter user"; 
$user = <STDIN>; 
chomp ($user); 
print "User is $user\n"; 

    } 

    case '2' 
{ 
print "Enter process:"; 
$process = <STDIN>; 
chomp ($process); 
print "Process is $process\n"; 
    } 
    } 
+1

http://perlmaven.com/global-symbol-requires-explicit-package-name – 2014-10-31 09:35:23

+1

请勿使用开关http://stackoverflow.com/questions/2630547/why-is-the-switch-module- deprecated-in-perl – 2014-10-31 09:37:22

+1

你的[编辑](http://stackoverflow.com/posts/26671362/revisions)完全改变了这个问题,你的缩进是可怕的。精益基本编程http://learn.perl.org – 2014-10-31 10:17:37

回答

1

如果使用perltidy格式化你的代码,你可以很容易地看到,有一个无与伦比的支架:

#!/usr/bin/perl 
use warnings; 

my $input=''; 

while ($input ne '3') { 
    clear_screen(); 

    print "1. user\n" . "2. process\n" . "3. exit\n"; 
    print "Enter choice: "; 
    $input=<STDIN>; 
    chomp($input); 
    { ## <-- this open brace is probably the culprit of the error 

     if ($input eq '1') { 
      print "Enter user"; 
      $user=<STDIN>; 
      chomp($user); 
      print "User is $user\n"; 
     } 
     else { 
      print "Enter process:"; 
      $process=<STDIN>; 
      chomp($process); 
      print "Process is $process\n"; 
     } 
    } 

代码格式不只是使代码的样子美观大方;这也使得更容易追踪错误并让其他人理解。您还应该在所有脚本上使用use strict;以确保在代码中出现问题之前,可能会发现编程错误。