专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

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

发布时间:2011-06-29 20:00:22 文章来源:www.iduyao.cn 采编人员:星星草
关于defined的使用
大家好,我写了一段程序,但是发现出现语法错误,请大家指点一下。谢谢!具体源码如下:
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呢?

$_的用法你知道吗?
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: