关于defined的使用
大家好,我写了一段程序,但是发现出现语法错误,请大家指点一下。谢谢!具体源码如下:
#!/usr/bin/perluse 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");
state $name; $_=shift; #加上这行就可以了 if(!defined($name))
[解决办法]
为啥要把$_给变量name呢?
$_的用法你知道吗?