大家好,我写了一段程序,但是发现出现语法错误,请大家指点一下。谢谢!具体源码如下:
- Perl code
#!/usr/bin/perl use 5.010; use strict; use diagnostics; use feature 'state'; sub greet { state $name; if(!defined($name)) #这里判断静态变量$name是否已经定义 { $name=$_; print "Hi ", $_, "! ", $name, " is also here ! \n"; } else { print "Hi ", $_, "! ", $name, " is also here ! \n"; } } greet("Fred"); greet("Barney");
系统提示:
Use of uninitialized value $_ in print at ./Ex4_4 line 13 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
请问这个原因是什么?
------解决方案--------------------
你把 @_ 和 $_ 弄混了
- Perl code
state $name; $_=shift; #加上这行就可以了 if(!defined($name))
------解决方案--------------------
为啥要把$_给变量name呢?
$_的用法你知道吗?