Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Template Toolkit and unpredictable hashrefs

by rastoboy (Monk)
on Mar 29, 2010 at 01:42 UTC ( [id://831538]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks! I'm having a devil of a time comprehending what Template::Toolkit wants from me. I have an object that resembles this:
$VAR1 = { '10.1.7.142' => { 'counter' => 2, 'clusterstatus' => on, }, '10.1.7.11' => { 'counter' => 3, 'clusterstatus' => off, }, '10.1.7.112' => { 'counter' => 3, 'clusterstatus' => on, }, };
As you can see, I'm collecting information about different IP's. I don't know in advance what they will be. I send the object itself to the template code, which looks like this:
[% FOREACH ip = data %] this is ip [% ip %] counter is [% ip.counter %] [% END %]
Which yields me:
this is ip is HASH(0x9c68328) counter is this is ip is HASH(0x9c62e68) counter is this is ip is HASH(0x9c62d0c) counter is
So I expect to get "HASH(0x...)" for the $ip variable, but I don't understand why I can't access ip.counter. I've even tried making a separate hashref with a simple list of ip's in the hash and iterating through that:
in the same object: 'ips' => [ '10.1.7.142', '10.1.7.11', '10.1.7.112' ],
and then:
[% FOREACH ip in ips %] this is ip is [% ip %] counter is [% ip.counter %] [% END %]
But with that I get nothing at all, like it can't find the 'ips' key. In the past when I'm using key names that are predictable, I haven't had any trouble, but this one is kicking my behind. It appears I'm misapprehending something basic--any input would be greatly appreciated!

Replies are listed 'Best First'.
Re: Template Toolkit and unpredictable hashrefs
by ikegami (Patriarch) on Mar 29, 2010 at 01:55 UTC
    From what I can tell from the docs,
    [% FOREACH ip = data %] this is ip [% ip %] counter is [% ip.counter %] [% END %]
    should be
    [% FOREACH ip = data %] this is ip [% ip.key %] counter is [% ip.value.counter %] [% END %]
      Dude. That is awesome, yes. Can you please link me to the particular docs you're looking at? I have actually been reading lots of docs and I haven't seen/perceived this syntax. Thank you so much, that worked great!
Re: Template Toolkit and unpredictable hashrefs
by Herkum (Parson) on Mar 29, 2010 at 14:06 UTC

    Your call is actually a method invocation,

    # Template Toolkit ip.counter # Perl ip->counter()

    So for the obvious reason, that should not return a result. To prevent the call from blowing up, TT will silently ignore these types of errors.

    As ikegami showed you how to fix your error I will offer you advice. Avoid doing data management or manipulation in your templates. The feedback mechanism for errors is poor and the syntax is harder to work with than pure perl. Move more of your data management into Template plugins where you can use pure perl to do the work. Otherwise prepare the data so it is easier to work with before you send it to the template. A good example is that I never use hashes in templates. Better to use objects as they will act like you expect.

Re: Template Toolkit and unpredictable hashrefs
by Mr. Muskrat (Canon) on Mar 30, 2010 at 17:16 UTC

    If I were tackling such a problem, I would use a slightly different data structure.

    my $ips = [ { ip => '10.1.7.142', counter => 2, clusterstatus => 'on', }, { ip => '10.1.7.11', counter => 3, clusterstatus => 'off', }, { ip => '10.1.7.112', counter => 3, clusterstatus => 'on', }, ];
    Then in your template:
    [% FOREACH item IN ips %] this is ip is [% item.ip %] counter is [% ip.counter %] [% END %]
      One advantage of this method is that you can control the order of the records.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://831538]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found