“无法对未定义的值调用方法'状态'”



我是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对未定义的值调用方法'状态'"。

也许没有启用state功能?引用文档:

仅当使用功能"state"杂注有效时,才会启用状态变量,除非关键字写为 CORE::state 。另请参阅功能。或者,将 v5.10 或更高版本的使用包含在当前范围内。

正如我在评论中读到的那样,您没有包括use 5.010的编译指示。我认为这就是问题所在。没有它,我的Perl(5.18.2,和你的一样(抱怨同样的错误。

最新更新