+ -
当前位置:首页 → 问答吧 → 关于defined的使用

关于defined的使用

时间:2011-12-03

来源:互联网

大家好,我写了一段程序,但是发现出现语法错误,请大家指点一下。谢谢!具体源码如下:
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.

请问这个原因是什么?

作者: chennut0802   发布时间: 2011-12-03

你把 @_ 和 $_ 弄混了 
Perl code


  state $name;
  $_=shift; #加上这行就可以了
  if(!defined($name))


作者: ccdbg   发布时间: 2011-12-03

1楼的朋友,加了还是不行啊,还是有问题。

作者: chennut0802   发布时间: 2011-12-03