RFC 229 (v2) Variable interpolation on demand.

[email protected] (Perl6 RFC Librarian) 29 Sep 2000 03:24:58 -0000
Newsgroups perl.perl6.announce.rfc,perl.perl6.language
Message-ID <[email protected]>
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/

=head1 TITLE

Variable interpolation on demand.

=head1 VERSION

  Maintainer: Glenn Linderman <[email protected]>
  Date: 14 Sep 2000
  Last Modified: 28 Sep 2000
  Mailing List: [email protected]
  Number: 229
  Version: 2
  Status: Frozen

=head1 ABSTRACT

Make Perl's powerful string interpolation facilities are available to
variables, in addition to literals.

=head1 CHANGES

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.

=head1 DESCRIPTION

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 );


=head2 Compatibility considerations

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.

=head1 IMPLEMENTATION

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.

=head1 REFERENCES

None.