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

Not_a_Number has asked for the wisdom of the Perl Monks concerning the following question:

I was experimenting with parsing a flat file like in __DATA__ below, and I tried out the following (NB the snippets below work with strict and warnings, removed here for brevity):

my %totals; while ( <DATA> ) { chomp; if ( /(^[a-z].*$)/i ) { $totals{$1} = 0 unless $totals{$1}; } else { $totals{$1} += $_; } } print "$_: $totals{$_}\n" for keys %totals; __DATA__ player1 11 22 11 player2 10 21 player1 22

This outputs, as expected(?):

player1: 66 player2: 31

Similarly, the following works:

while ( <DATA> ) { if ( /^([A-Z]+)$/ ) { print "In if: $1\n"; } else { print "In else: $1\n"; } } __DATA__ FOO 1234 xyz BAR dfgdfg

Output:

In if: FOO In else: FOO In else: FOO In if: BAR In else: BAR

But if I change my data to:

__DATA__ FOO 1234 Xyz BAR DFGdfg

(Note capitalisation changes in lines 3 and 5), the output is:

In if: FOO In else: FOO In else: F In if: BAR In else: B

...whereas I would have expected the result to be the same.

From perldoc perlre:

The numbered variables ($1, $2, $3, etc.) <snip> are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first.
But $1 can't have gone out of scope (or could it?) or the original snippet wouldn't have worked; and there can't have been a 'next successful match' (or could there?) since the code fell through to the else clause??

Additionally, if I change (for example) my last data item to a string that begins with something other than a capital letter but then contains at least one capital (eg 'dFgdfg' or '2FGdfg)', I get an 'uninitialized value' warning, because $1 is empty.

Since I get no warnings for 'Xyz' and 'DFGdfg', why does $1 apparently just contain the first capital letter of the previous match in these cases?

Or am I missing something obvious?

(Tested on Win XP/AS 5.61 and Mandrake Linux/5.81)

TIA

Yours,

Confused of Paris

Edited by Chady -- added readmore tags.