2014-10-31 37 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;以確保在代碼中出現問題之前,可能會發現編程錯誤。

相關問題