Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Trouble building hashes from value lists inside a TT2 template

by monsieur_champs (Curate)
on Dec 19, 2004 at 12:53 UTC ( #415979=note: print w/replies, xml ) Need Help??

in reply to Trouble building hashes from value lists inside a TT2 template

Dear fellows
I'm posting this here hopping this can help other monks too. Guess I was needing a good sleep night.

As soon as I awake this morning, I devised the solution. Its just a matter of making less de-references against the "item" variable, like this:

[% USE pessoa_fisica = Class('reciclar::cdbi::pessoa_fisica'); FOREACH item = [ "id", "primeiro_nome", "sobrenome", "display_name" +, "cep", "nome_logradouro", "numero", "cidade", "estado", "ddd", "tel +efone" ]; IF $item.defined(); THEN; # # Here was my problem. # I couldn't see that I needed the same construct in # both sides of the assignment operation, because the # de-reference operation have different behaviors # when used inside a hash lookup and alone, in kind # of "scalar context". # params.$item = $item; END; END; IF params.keys(); THEN; list_pessoa_fisica = pessoa_fisica.search_where( params, { logic => "and" } ); ELSE; list_pessoa_fisica = pessoa_fisica.retrieve_all(); END; %]

If you pay attention, there is two ways of using the $item construct. First, in "scalar context" (kind of), the result is the transformation of a string in a variable name (and the consequent use of its value in place of the string representing the variable's name). Second, in "hash context" (sorry about the poetic license) when you need to use a variable to name a hash key, in a construct like $hash{$key}, and the interpolation uses the variable name as the hash key (using the same construct used to get a variable's value instead of his name in "scalar context".

I became confuse with those two applications of the same syntatic construct and the mess was done. :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://415979]
[james28909]: what i am trying to do is track state of a chat based application. i need to be able to store the channels and such for a user, and when they join a new server/channel i need to be able to update that feild.
[james28909]: do you think OO would best suite that?
[karlgoethebier]: james28909: The last two are not mainstream. You have been warned ;-)
[karlgoethebier]: james28909: I don't know. Try it.
[james28909]: foreach server this bot is on, the gateway sends me a json response, which i parse out all the info with JSON, and this response has user information, i need to be able to add a user, and then update the user info when they join/leave rooms ect

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2017-05-25 16:35 GMT
Find Nodes?
    Voting Booth?