In Perl, what's the difference between "if defined $count" and "if $count"?

Viewed 3544

I have the following script:

#!/usr/bin/perl
use warnings;
use strict;

my $count = 0; 
my ( @first , @second , @third );

while ($count <= 7){

    push ( @first , $count);
    push ( @second , $count) if defined $count;
    push ( @third , $count) if $count;
    $count++;

}

print "first: @first\n";
print "second: @second\n";
print "third: @third\n";

This produces the following output:

first: 0 1 2 3 4 5 6 7
second: 0 1 2 3 4 5 6 7
third: 1 2 3 4 5 6 7

What's the difference between putting if defined $count vs. if $count, other than the latter method won't add the zero to the array? I've searched the perldocs but couldn't find the answer.

6 Answers
Related