Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Trouble building hashes from value lists inside a TT2 template

by monsieur_champs (Curate)
on Dec 18, 2004 at 23:05 UTC ( #415917=perlquestion: print w/replies, xml ) Need Help??

monsieur_champs has asked for the wisdom of the Perl Monks concerning the following question:

I'm having big trouble with TT2 and a template that needs to implement data filters. As the files I wrote are big and kind of senseless, I'm posting just the bytes that matter.

[% # # Template "tbl_pessoa_fisica.tt2" # USE pessoa_fisica = Class('reciclar::cdbi::pessoa_fisica'); # The values in the FOREACH list are all variable names # provided by my template renderization engine. It works # receiving some parameters and is well-tested, as I've # used it unmodified in many projects before this. # FOREACH item = [ "id", "primeiro_nome", "sobrenome", "display_name" +, "cep", "nome_logradouro", "numero", "cidade", "estado", "ddd", "tel +efone" ]; # IF ${$item}.defined(); THEN; ### doesn't work for me # IF ${$item} != ""; THEN; ### doesn't work for me IF ${$item}; THEN; ## Should do the same of perl's "$params{$item} = ${$item};" params.$item = ${$item}; END; END; ### Here is my problem: ### params.keys() never returns a key list (hash is empty!), ### despite what I pass as parameters. IF params.keys(); THEN; list_pessoa_fisica = pessoa_fisica.search_where( params, { logic => "and" } ); ELSE; list_pessoa_fisica = pessoa_fisica.retrieve_all(); END; %]

Things put short, I need to construct a parameter list based on a bunch of variables that I'll receive inside the template from the Perl script running it. I can't accept blank and undef() values into this hash, so I can pass it as my Class::DBI::AbstractSearch::search_where() parameter list.

My problem is that doesn't matter what test I use, nor what values are being passed into the scritp, I just can't build the hash. Any fellow monk have any past experience, tips, tricks, suggestions or code to offer?

Replies are listed 'Best First'.
Re: Trouble building hashes from value lists inside a TT2 template
by monsieur_champs (Curate) on Dec 19, 2004 at 12:53 UTC

    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:

    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://415917]
Approved by bart
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2023-03-27 01:10 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.