in reply to Re: Re: Re: Re: Re: Re: the search string and me
in thread the search string and me

{nods in agreement} I do use my $params{'things'} in strings and concat to strings 'bit '.$params{'bot'} however:

My templating function does this nattly little conversion of a field name into its value in a string:

$fld =~ s/\$(\w+)/${$1}/g; # convert variable into its value..
Which failed the first time I tried including a $params{'thing'} so I gave up and just used $scalars ever since :-)

I should re-investigate.. sounds like a good idea!

___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: Re: the search string and me
by bart (Canon) on Sep 22, 2003 at 07:03 UTC
    Ah but your templating system is using global variables for the substitution, which is why it doesn't work with a hash. If you see "$(things)" you try and replace it with the contents of $things. And that's using symbolic references, which is a bad idea. See these 3 posts by dominus for an explanation why symbolic references are a very bad idea, in general. The moral of it is that it's very easy for them to clash with vital variables for your program. It's like driving a car with your passengers seated on your engine.

    Why don't you build your templating system around a hash? Store every value you ever want to use in one hash, let's call it %var, and do the substitution like this:

    $fld =~ s/\$(\w+)/$var{$1}/g;
    Dead easy. You can simply use %params as your hash, or copy the desired values to %var first:
    %var = (%params, foo => 'this is a new or changed field', bar => 'and + another', greet => 'Hello');
    If your template contains "$(greet)" it'll get replaced by "Hello".
      Thats a good idea, instead of converting my hashes into scalars, convert any stray scalars that need displaying back into the hash!

      And I had no idea those fun creatures called hashes, which I dearly love and use with relish, could be concat in such a way:

      %var = (%params, foo => 'this is a new or changed field', bar => 'and + another', greet => 'Hello');
      Thanks!
      ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com