Hello, I was reading stuff on the perl6 web site, and had some ideas about
string interpolation rules.  Is this a place to send this?


String interpolation should be controlled by the programmer on a string by
string basis, or on more global quote-type by quote type basis.

---------------
scenario one, object.method format
---------------

Lets pretend a string is an object.  The "normal" value of the string is the
interpolation of the string using the default rules for the quotes used.

Lets add an .interpolate method.  The parameter(s) are rules that control
the interpolation, and the returned value is the interpolated string using
those rules.

        $result = 'scalar $vars (only) will be interpolated' .
interpolate($) ;

The .interpolate method would have the additional ability to interpolate the
contents of a variable.
        $result = $string.interpolate;  # some type of interpolate, probably
like "
        $result = $string.interpolate(${});     # interpolate scalar vars
surrounded by {}'s


you could imagine allowing more complex rules, such as mapping
interpolations to user defined functions, or only interpolating defined
variables, otherwise leave them as-is, or perhaps allowing you to map
specific variables to values but just for that one string's interpolation
        # only interpolate defined variables
        $result = "$some $of $these $will $be
$interpolated".interpolate(defined($)) ;

        # interpolate, one variable gets a specific value
        $result = "$one $two $three".interpolate('$one'=>'hi there') ;

        # interpolate, scalar values come from user defined function
        $result = "$one $two $three".interpolate($ => \&my_function) ;

Here's some more examples

                # normal interpolation (i.e. none in ' quotes)
        $result = 'string with $var';

                # override the ' defaults
                # use the $ rule, which would mean interpolate scalars just 
                # as we do today in " strings
        $result = 'string with $var' . interpolate($);

                # use the ${} rule, which would mean interpolate scalars,
but
                # only when bracketed (using {})
        $result = 'string with ${var}' . interpolate(${});

                # allow a function to interpolate in scalar context
                # by using the & rule combined with $-with-round-brackets
rule
                # the function parameters would not be interpolated,
        $result = 'string with $(my_function($one,$two))' .
interpolate($(&));

                # allow a function to interpolate in array context
                # by using the & rule combined with @-with-round-brackets
rule
        $result = 'string with @(my_function($one,$two))' .
interpolate(@(&));

                # allow a function to interpolate in scalar context
                # by using the & rule combined with $-with-round-brackets
rule
                # and also let the function parameters be interpolated
        $result = 'string with $(my_function("$one","$two"))' .
interpolate($(&));      

                # nested interpolation rules. 
                # We force regular scalar interpolation on one of the
parameters
                # overriding the default ' interpolation rules
        $result = 'string with $(my_function('$one'.interpolate($)
,'$two'))' 
                        . interpolate($(&));    


You could set the default interpolation on a quote-type basis for a scope.

                # allow qq{} to always do scalar, array, and function
interpolation,
                # but only if the scalar is surrounded by //, the array by
{}, and
                # the function by <>

        use interpolate 'qq' => '$//@{}$<&>' ;

        $result = "$this does *not* @interpolate $(at{'all'})"; 
        $result = "$/this/ *does* @{interpolate} $<at{'all'}>";


---------------
scenario two, same idea but using =~ notation.
---------------

A string could have interpolation rules "applied" to the string.  The rules
would not modify the string, just the value seen by when the value is
extracted.  That's not an issue in a simple quoted string, but makes a
difference if you interpolate a variable

        $result = "this $is_not interpolated, but ${this_is}" =~ i/${}/;

        $result1 = $master_copy =~ i/${}/;      # get the result of
interpolating ${} vars
        $result2 = $master_copy =~ i/@/;        # get the result of
interpolating @ vars



$0.02

Reply via email to