Damian Conway <[EMAIL PROTECTED]> writes:

> Brad Bowman asked:
>
>> When building code strings in Perl 5 I usually write the code,
>> then wrap it in double quotes, then "\" escape everything light blue
>> under syntax highlighting.  I was wondering if there'll a better
>> way in Perl 6. I thought it would be nice to define the variables
>> you wish to
>> interpolate individually, perhaps as extensions to the :s, :a,
>> etc quote adverbs, perhaps using a signature object.
>
> There is already a mechanism for this. You simply turn off all
> variable interpolation, and interpolate any the variables you wish to
> interpolate via block interpolations. Or, more simply, only turn on
> block interpolation in a non-interpolating string:
>
>      my $code = q:c{
>          package {$package_name};
>
>          sub {$sub_name} \{
>             return {$return_val}
>          \}
>      };

The problem here is that sometimes (especially in code generation) you
don't really want to interpolate the stringification of a value, you
just want the value itself. I've been struggling with this slightly
when I've been writing code generation methods in Ruby. Where, in
Scheme or lisp you'd simply use some combination of C<`>, C<,> and
C<,@>, in Ruby you end up having to store values somewhere in the
binding that will be accessed by your compiled code; which would be
fine if (again, as with Lisp macros) you could generate what I tend to
think of as an anonymous symbol to bind the value to:

   my $anon_symbol = gensym
   my ${$anon_symbol} = $the_complex_value_we_want_to_use_in_generated_code

   my $code = q:c{
       package {$package_name};

       sub {$sub_name} \{
           return ${$anon_symbol}
       \}
   };

And backwhacking braces in generated code is *not* a pretty solution
to my eyes. I'd *like* to be able to have a quasiquoting environment
along the lines of lisp's backquote (though I'm not sure about the
unquoting syntax):

   my $code = q:`{
       package ,$package_name;

       sub ,$sub_name {
           ,$the_complex_value_we_want_to_use_in_generated_code 
       }
       
       sub ,$another_sub_name {
           ,[EMAIL PROTECTED](';')}
       }
   };

Whatever we go with, we need a quoting mechanism that returns a parse
tree rather than a simple string if we want to deal with interpolating
complex values (especially if we don't want to have to worry about
what, if any, quotes are needed round some of our interpolated values.

-- 
Piers Cawley <[EMAIL PROTECTED]>
http://www.bofh.org.uk/

Reply via email to