2015-12-09 44 views
-1

我是perl的新手。我正在從一本書中學習,那裏有一個例子。我無法找到那個問題。「不能調用未定義值的方法」狀態「?

use 5.010; 
greet('Fred'); 
greet('Barney'); 
sub greet { 
    state $last_person; 
    my $name = shift; 
    print "Hi $name! "; 
    # This is the error line: 
    if(defined $last_person) { 
     print "$last_person is also here!\n"; 
    } else { 
     print "You are the first one here!\n" 
    } 
    $last_person = $name; 
} 

它給錯誤,如「不能調用方法‘國家’在ch4_3.pl未定義的值。」

+0

'perl -v'說什麼? – zoul

+0

我沒有寫使用5.010。礦是5.18.2。 – Raj

+7

@Raj - 「我沒有編寫使用5.010」 - 因此,您在問題中提供的代碼與提供錯誤消息的代碼不同。你刪除了使用聲明? – Quentin

回答

6

也許state功能未啓用?引用the docs

狀態變量才能啓用了使用功能「狀態」編譯生效,除非關鍵字寫爲CORE ::狀態。另請參見功能。或者,在當前範圍中包含v5.10或更高版本的使用。

正如我在評論中看到的,你沒有包含use 5.010編譯指示。我認爲這是問題。沒有它,我的Perl(5.18.2,與你的一樣)抱怨同樣的錯誤。

+1

但是如果我們使用5.18.2的更高版本,那麼我們是否需要編寫5.010? – Raj

+3

@Raj - '使用5.010'的意思是「打開所有在5.10中被認爲是穩定的功能」(我個人更喜歡「use v5.10」的語法)而不是「只適用於Perl 5.10,沒有更新的東西」。 – Quentin

相關問題