Subject: Re: Macro to enable Perl-style strings
From: Erik Naggum <erik@naggum.no>
Date: 18 Jan 2004 21:26:17 +0000
Newsgroups: comp.lang.lisp
Message-ID: <3283449977481024KL2065E@naggum.no>

* Nepheles
| I hacked together a short macro to enable you to use Perl-style
| strings in Lisp. I.e., something like "Today is $DAY" will have $DAY
| replaced with the (symbol-value) of DAY.  [...] Unfortunately, this
| forbids lexical variables.

  Let the caller of the macro evaluate the expression, instead.

  E.g., the macro call (evalstr "mumble $foo $bar $zot mumble") should
  return (concatenate 'string "mumble " foo " " bar " " zot " mumble").

  You should use READ-FROM-STRING to pick up the symbol names from the
  string, not try to parse and hack things together yourself.  Just give
  it a :START argument of the position following the $.  Then you can
  use any Common Lisp expression you want after the $, not just simple
  variable names.  I recommend :PRESERVE-WHITESPACE T, so any whitespace
  that terminates the expression is part of the string literal.  This
  function returns the position following what it just read.

  Once you have this working, consider using a reader macro, instead, so
  that, e.g., $"mumble $foo $bar $zot mumble" returns the expression at
  read-time.  Some languages need special syntax to prevent the name of
  the variable from running into the following text.  If you follow my
  suggestions, you can use "foo$(values bar)zot" or any other expression
  that returns the evaluated value.  (Note that QUOTE does not.)  But if
  you're really into syntactic sugar, you could write a reader macro for
  {...} that returns (values ...).

  I have not looked at the pre-existing solutions for interpolating
  strings, but I would be surprised if they did not do most if not all
  of the above.

-- 
Erik Naggum | Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.