首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

关于defined的使用,该如何解决

2012-03-01 
关于defined的使用大家好,我写了一段程序,但是发现出现语法错误,请大家指点一下。谢谢!具体源码如下:Perl c

关于defined的使用
大家好,我写了一段程序,但是发现出现语法错误,请大家指点一下。谢谢!具体源码如下:

Perl code
#!/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");


系统提示:
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呢?

$_的用法你知道吗?

热点排行