有一个perl程序,中state的用法错误程序如下:/usr/bin/perluse strict;sub greet{state @people;my $num;foreach (@_){push (@people,$_);}$num = @people;if($num == 1){print "\n你是第一个来的人!\n";}else{print "\n$people[$#people-1]已
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/20 00:31:14
有一个perl程序,中state的用法错误程序如下:/usr/bin/perluse strict;sub greet{state @people;my $num;foreach (@_){push (@people,$_);}$num = @people;if($num == 1){print "\n你是第一个来的人!\n";}else{print "\n$people[$#people-1]已
有一个perl程序,中state的用法错误
程序如下:
/usr/bin/perl
use strict;
sub greet{
state @people;
my $num;
foreach (@_){
push (@people,$_);
}
$num = @people;
if($num == 1){
print "\n你是第一个来的人!\n";
}else{
print "\n$people[$#people-1]已经比你先到了 \n";
}
}
###开始进行一些测试#########################
&greet("tom");
#greet("Lily");
#greet("wangzi");
报错:
Array found where operator expected at ./Geet1.pl line 5,at end of line
(Missing operator before )
syntax error at ./Geet1.pl line 5,near "state @people"
Global symbol "@people" requires explicit package name at ./Geet1.pl line 5.
Global symbol "@people" requires explicit package name at ./Geet1.pl line 8.
Global symbol "@people" requires explicit package name at ./Geet1.pl line 10.
Global symbol "@people" requires explicit package name at ./Geet1.pl line 14.
Global symbol "@people" requires explicit package name at ./Geet1.pl line 14.
Execution of ./Geet1.pl aborted due to compilation errors.
有一个perl程序,中state的用法错误程序如下:/usr/bin/perluse strict;sub greet{state @people;my $num;foreach (@_){push (@people,$_);}$num = @people;if($num == 1){print "\n你是第一个来的人!\n";}else{print "\n$people[$#people-1]已
state这个是在perl 5.0.10里面提供的功能,
需要明确指明,才可以使用,如何指明:
加入这行:
use feature qw(state);
或者加入这行:
use 5.010;
#!/usr/bin/perl
use strict;
use 5.010;
sub greet{
state @people;
my $num;
foreach (@_){
push (@people, $_);
}
$num = @people;
if($num == 1){
print "\n你是第一个来的人!\n";
}else{
print "\n$people[$#people-1]已经比你先到了 \n";
}
}
###开始进行一些测试#########################
&greet("tom");
&greet("Lily");
&greet("wangzi");
你是第一个来的人!
tom已经比你先到了
Lily已经比你先到了
试下去掉use strict; 或者 添加use feature 'state' ;
在perldoc里有这么一段:"state" variables are only enabled when the "feature 'state'" pragma is in effect
所以,你想用state的话,就需要加入这行:
use feature 'state' ;
use 5.010
state是5.10后出来的