good chemistry is complicated,
and a little bit messy -LW
What's happening to my $1?by Not_a_Number (Parson)
|on Jan 20, 2004 at 20:25 UTC||Need Help??|
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):
This outputs, as expected(?):
Similarly, the following works:
But if I change my data to:
(Note capitalisation changes in lines 3 and 5), the output is:
...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)
Confused of Paris
Edited by Chady -- added readmore tags.