[% setvar title Variable interpolation on demand. %]
Note: these documents may be out of date. Do not use as reference! |
To see what is currently happening visit http://www.perl6.org/
Variable interpolation on demand.
Maintainer: Glenn Linderman <glenn@linderman.com> Date: 14 Sep 2000 Last Modified: 28 Sep 2000 Mailing List: perl6-language@perl.org Number: 229 Version: 2 Status: Frozen
Make Perl's powerful string interpolation facilities are available to variables, in addition to literals.
Version 2 adds more description to the IMPLEMENTATION section.
Notes on Freeze: not much discussion resulted on the list. Someone suggested the implementation now mentioned in the implementation section, but it isn't really general enough due to the need to choose a delimiter character, although it works for some strings some of the time.
Given:
$foo = 'def'; $bar = 'ghi'; $x = "abc$foo$bar"; $y = 'abc$foo$bar';
There is no way to turn obtain the value of $x from the value of $y. In other words, while $foo and $bar were interpolated into $x, they were not interpolated into $y. It would be nice to get Success! from:
$z = interpolate ( $y ); print 'Success!" if $z eq $x;
However, there is no direct language facility for this, because interpolation only happens one level deep (which is good), so attempts such as
$z = qq/$y/;
will interpolate $y, but not $foo and $bar, so $z eq $y, instead of $z eq $x.
Shown above is a functional syntax. However, due to scoping rules and lexical variables, it is unlikely that this functionality could be implemented as a function, more likely it would have to be a keyword. An alternate syntax might be:
$z = qd/$y/;
implying a double interpolation pass. Or
$z = q2/$y/;
which means 2 interpolation passes, and which could be extended to q3, q4, q5 ??? Or
$z = qq/$y/2;
which likewise would mean two passes, and could be extended to 3, 4, 5, or even to
$z = qq/$y/$n;
a variable number of passes. The same could be achieved with the keyword technique via an additional parameter:
interpolate ( $y, $n );
This is a new feature, so name conflict is the only issue.
This seems compatible with other extensions to string interpolation... whatever extensions get implemented should work here too.
Just a re-write rule.
interpolate ( $y )
could become
eval "qq\000".$y."\0";
if you make the assumption that null characters would never exist within $y. Of course, that is an invalid assumption in Perl. Avoiding that assumption would either require that you scan $y and discover a character not contained within it that can be used for the delimiter, or instead of the eval, make a direct call to the interpolation function inside Perl. This latter is probably the better solution, and explains why this should be in core.
None.