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

I only added the eval bit as I got tired of having to manually convert $username = $params{'username'}; each time I wanted to display the username variable (again my own code which replaces tagged fields with variables and I was unable to get it to handle hash or list values but it was happy with scalars).
Huh? That's odd. This is Perl, not PHP, and you can use hash items in a string just like any other scalar. Just use the correct type of quotes, or no quotes for a bareword (= just like the name of a plain variable), or escape them with a backslash. Like this:
$params{'name'} = "Slim Shady"; print "My name is $params{'name'}\n"; print "My name is $params{name}\n"; print "My name is $params{\"name\"}\n";
All three work equally well.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: the search string and me
by wolis (Scribe) on Sep 22, 2003 at 02:57 UTC
    {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
      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