http://www.perlmonks.org?node_id=942083


in reply to Re: scoping problem?
in thread scoping problem?

Does the text file contain a blank line?

rocroc:
Here's an example of behavior when processing a blank line or a line having empty or too few fields: an empty or missing field splits to an empty string or to undef. The reason for the seemingly spurious match is that an empty string or an undef becomes the // regex, which always matches!

>perl -wMstrict -le "my $s = 'foo,,'; ;; my ($field0, $field1, $field2, $field3) = split /,/, $s; print qq{field0 '$field0' field1 '$field1' field3 '$field3'}; ;; if ('bar' =~ m{ $field1 }xms) { print qq{bar matches '$field1'} } if ('bar' =~ m{ $field3 }xms) { print qq{bar matches '$field3'} } " Use of uninitialized value $field3 in concatenation (.) or string ... field0 'foo' field1 '' field3 '' bar matches '' Use of uninitialized value $field3 in regexp compilation ... Use of uninitialized value $field3 in concatenation (.) or string ... bar matches ''

Note that without warnings (you are using warnings, aren't you?), this all proceeds quite silently:

>perl -Mstrict -le "my $s = 'foo,,'; ;; my ($field0, $field1, $field2, $field3) = split /,/, $s; print qq{field0 '$field0' field1 '$field1' field3 '$field3'}; ;; if ('bar' =~ m{ $field1 }xms) { print qq{bar matches '$field1'} } if ('bar' =~ m{ $field3 }xms) { print qq{bar matches '$field3'} } " field0 'foo' field1 '' field3 '' bar matches '' bar matches ''