in reply to Re: use of uninitialized value in hash element problem
in thread use of uninitialized value in hash element problem

chomp $readline; my ($key , $value) = (split /::/, $readline); defined $key and $data_ids{$key} = $value;

Which is the same as saying:

chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids{$key} = $value if $key;

Which is the same as saying:

chomp $readline; if ( my ($key , $value) = (split /::/, $readline) ) { $data_ids{$key} = $value; }

Sorry, but that's wrong. The three code variants have similar, but not identical effects:

The first one stores any defined key (and value) in %data_ids, including the empty string and 0 (zero), but excluding undef. The second one stores only those keys in %data_ids which are true in boolean context. This excludes the empty string, 0, and undef. The third variant stores any key as long as split returns at least one value. This includes the empty string, 0, and undef.

Also, the scope of $key and $value is limited to the if block in the third variant, but not in the first and second variant.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: use of uninitialized value in hash element problem
by 1nickt (Canon) on Jun 26, 2015 at 13:26 UTC

    Agreed. Should not have said "the same as." Tried to oversimplify.