RE: Recursive Interpolation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Chuck Wolber wrote:
> On Mon, 4 Oct 2004, Michael Sims wrote:
>> What's ugly about it?  I saw your earlier post I was actually
>> planning on responding and suggesting something exactly like you
>> just came up with.
>
> The main problem (aside from performance, which you addressed) is
> that it does not handle corner cases such as when you want to use one
> of your variables within the text of the dispatch (escaping).

I think the answer to that is to use a token delimiter that is unlikely to ever
appear literally.  In my implementation I look for tokens that appear between ?: and
:?.  The token is only replaced if it appears in my data array.  The likelihood that
I'll need a literal word surrounded by my delimiters that is also an existing token
in my array is low enough to be negligible, IMHO.

> It is
> also not truly recursive.

Maybe I'm not fully understanding the problem, but I don't see where recursion
enters into it.  Do you want the interpolate() function to call itself again when it
comes across uninterpolated tokens within a token?  Even PHP's own double-quote
interpolation doesn't work like that.  In the string "this is a $variable" the
$variable has to have already been defined at the time the string is evaluated...IOW
you cannot delay interpolation even in pure PHP code without using eval().  Can you
explain what you mean by "truly recursive" and give an example where it might be
useful?

> From a hacker standpoint, it extends the base PHP tool set, rather
> than making use of existing tools (not necessarily a bad thing). IMHO
> a non-ugly version of this would allow you to use standard PHP
> variables in your dispatch and force PHP itself to re-interpret the
> variables. This is why I think an interpolate() function would have
> to be built in to the base PHP interpreter rather than be done with
> the language itself.

I can see a value in having such a function, even if I personally wouldn't use it to
solve the problem you have described.  At least with my current, possibly incorrect
understanding of it. :)

> Any
> developers on the list want to comment on a possible interpolate()
> function?

Most of the PHP devs don't read this list.  You could post to the internals list,
although as another poster has already pointed out, this has been discussed there
already:

http://marc.theaimsgroup.com/?l=php-dev&m=109397509903069&w=2

The poster in that thread used eval() for this, which is another option if you
haven't already considered it.

>> I toyed around with using preg_replace_callback() for this eariler,
>> but the only problem with it is that you can't create a callback that
>> accepts more than just one variable (the array of matches).  This
>> means I couldn't get the equivalent of your $msg_variable passed to
>> the callback, so I had to make it global in the callback function
>> (yuk). If it weren't for that it'd be a perfect solution, because
>> preg_replace_callback would only be called once and the function
>> wouldn't need to iterate through all of $msg_variable. It's times
>> like that that I miss Perl's more powerful variable scoping rules.
>> Ah well...
>
> Definitely cleaner and even appears (at first glance) that it lends
> itself to being recursive a lot easier than mine. Could you solve the
> global problem by doing a $this.msg_variable sort of thing within the
> callback?

No.  You can use a method for the callback, but it has to be static, which means no
instance methods therefore you're still stuck with globals.  Here was my version,
FWIW (watch for wrapping):

function processTemplate($text) {
  return preg_replace_callback(
    '/\?:(.*?):\?/',
      create_function(
        '$matches',
        'global $values; return isset($values[$matches[1]]) ? $values[$matches[1]] :
$matches[0];'
      ),
    $text
  );
}

Having to use a global bothers me far more than calling preg_replace() multiple
times. YMMV.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux