[% setvar title Interpolation of subroutines %]
Note: these documents may be out of date. Do not use as reference! |
To see what is currently happening visit http://www.perl6.org/
Interpolation of subroutines
Maintainer: Michael G Schwern <schwern@pobox.com> Date: 17 Sep 2000 Mailing List: perl6-language@perl.org Number: 252 Version: 1 Status: Developing
Subroutines calls should interpolate in double-quoted strings and similar contexts.
print "Sunset today is at &sunset($date)";
interpolates to:
print 'Sunset today is at '.sunset($date);
Currently, subroutine calls cannot be placed directly into strings. This can often complicate the generation of a string and place the code to generate pieces of it distant from where they are put together causing maintenance problems. Also, with scalars, arrays, hashes and methods interpolating (or proposed to do so), users would expect subroutines to work.
Work arounds are as RFC 222.
Therefore, it is proposed that subroutine calls interpolate in double-quoted strings and similar constructs.
print "Sunset today is at &sunset($date)";
should parse out as:
print 'Sunset today is at '.sunset($date);
implementing DWIMness. The & is mandatory. Parens are also mandatory if arguments are to be passed.
Context and what to do with lists are as RFC 222.
Argument passing is as RFC 222.
Whitespace between the &, function name and opening paren should be disallowed when interpolated. This will avoid many ambiguous cases.
s/&/\\&/g in double quoted strings.
&foo() does not honor prototypes. Should "&foo()"? The only thing keeping me from saying yes is the apparent inconsistency.
Should the function not exist in the current scope, Perl will throw an exception/die as usual.
This RFC is intended to generate discussion about the need and wisdom of allowing subroutine interpolation in strings. Practical examples of code where this is useful as well as where this would be a hinderance are requested. It is presented seperately because I expect much more discussion than RFC 222 and wish to keep the consideration of the two seperate.
The tokenizer can watch for /&[A-Z_]\w*/i. If followed by a '(', then the parsing becomes the same as for normal subroutine arguments.
RFC 222 - Interpolation of object method calls
RFC 237 - Hashes should interpolate in double-quoted strings
RFC 251 - Interpolation of class method calls