in reply to Re^6: In place search and replace with a hash
in thread In place search and replace with a hash
OK, I take your point. Scalar interpolation is a narrower concept than I had thought. I assumed it meant replacing a variable name with its value, but perlglossary defines it as:
The insertion of a scalar ... value somewhere in the middle of another value, such that it appears to have been there all along. In Perl, variable interpolation happens in double-quoted strings and patterns, ...
And in perlop I found:
Interpolated scalars and arrays are converted internally to the join and . catenation operations. Thus, "$foo XXX '@arr'" becomes:$foo . " XXX '" . (join $", @arr) . "'";
I hadn’t thought about it that way before.
So, you’re correct in that:
The RHS of a substitution is interpolated, and single quotes do not suppress this (unless they are used as the delimiter).
The key field in a hash lookup $hash{ ... } is not automatically interpolated, since if it were, $h{$a$b} would be parsed as $h{$a.$b}.
Of course it is the case that an unquoted bareword is automatically stringified under certain conditions: $h{abc} is OK, but $h{2de} and $h{f g} are syntax errors. In fact, it looks as though the rules for stringifying a hash lookup key are the same as for stringifying the LHS of the => operator:
The => operator is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores.
— Comma Operator
But when you say:
But the keys of a hash-fetch are not interpolated they are executed, ...
I don’t think “executed” is the right term here: it sounds like you are saying they are evaled, which of course they are not. I would say rather: they are implicitly stringified (providing they meet the criteria detailed above) unless they have already been explicitly stringified, either by double-quotes ("..." or qq[...]) or by concatenation.
Thanks for raising some interesting distinctions.
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: In place search and replace with a hash
by LanX (Saint) on Dec 28, 2014 at 13:27 UTC |