Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: scoping problem?

by AnomalousMonk (Abbot)
on Dec 06, 2011 at 19:19 UTC ( #942083=note: print w/ replies, xml ) Need Help??


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 ''


Comment on Re^2: scoping problem?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942083]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2015-07-07 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls