2

I have a piece of code which does not work as I expect it to work. MAinly 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?

codaddict
  • 445,704
  • 82
  • 492
  • 529
user582452
  • 23
  • 2

1 Answers1

10

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.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 4
    @user582452 - a data dumper (such as Data::Dumper - http://search.cpan.org/dist/Data-Dumper/ ) is your friend. `print Dumper(\@jobs)` would have shown you what was in your array – plusplus Jan 20 '11 at 10:47