|
|
| laziness, impatience, and hubris | |
| PerlMonks |
How Perl decides where a variable ends and text starts: Match variables in string interpolationby davido (Cardinal) |
| on Jul 22, 2021 at 22:14 UTC ( [id://11135319]=perlquestion: print w/replies, xml ) | Need Help?? |
|
davido has asked for the wisdom of the Perl Monks concerning the following question: Once in awhile Perl still surprises me. This code is obviously broken:
The problem is that interpolation causes Perl to want to print a variable $left_ and $right, but we declared $left and $right. There are several ways to fix this, two of which are:
...and of course you could just use the concatenation operator, but then I wouldn't have anything to puzzle over. But observe the following:
The output is ab_cd. So in this case Perl automatically treated that interpolation as "${1}_$2\n" without me telling it to do so. I've looked over The Gory details of parsing quoted constructs and haven't found an explanation. I assume that in the case of numbered regex variables, Perl decides that if the variable starts with a numeric digit, the identifier must end when there are no more numeric digits. Is this behavior reliable? Is it documented? Is it likely to ever change? I'm asking because I found it in a code review and was sure it was broken until we talked it over and tested to verify the behavior was to parse "$1_$2\n" as $1 . '_' . $2 . "\n" even though we would prefer to disambiguate using \ or ${n}. Update: I do see in perldata: Identifier-parsing:
I don't know that this is worded quite right, because $1_ could be construed as NOT being an identifier consisting solely of digits. But it's the closest thing I can find to an explanation. But I'll take that as answering my own question: Yes, it's intentional and documented behavior. Dave
Back to
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||||||||||||