开发者

Why does defined function not work?

开发者 https://www.devze.com 2023-02-05 21:33 出处:网络
I have a piece of code which does not work as I expect it to work. MAinl开发者_如何学编程y the defined function does not work.

I have a piece of code which does not work as I expect it to work. MAinl开发者_如何学编程y the defined function does not work.

@jobs = qw[job1 undef job2];
if(defined($jobs[1])) {
  print "Job 1 is defined";
}

I get the output

Job 1 is defined

clearly $jobs[1] is undef. What am I missing?


Since you are using qw, your code is equivalent to:

@jobs = ("job1", "undef", "job2");

So $jobs[1] is the string "undef" which is not same as undef and hence the behavior.

If you want the second job to be an undef you can do:

@jobs = ("job1", undef, "job2");

AFAIK you cannot get this done using qw.

0

精彩评论

暂无评论...
验证码 换一张
取 消