[dancer-users] Only even items

David Precious davidp at preshweb.co.uk
Mon Apr 8 20:50:11 BST 2013

On Mon, 08 Apr 2013 21:39:45 +0200
Hugues Max <huguesmax at gmail.com> wrote:

> Thanks you, very much,  It's works
> but I not really understand why with hash , half of result go to keys 
> and other to values

Basic Perl - a hash is defined as a list of key, value, key, value.

You normally see it written as ( key => value ...), but "=>", the fat
comma, is actually syntactically equivalent to a normal comma (with
the exception of automatically quoting its left operand), it's just
used in this case as it's clearer.

So, the following two are equivalent:

my %hash = ( foo => 'Bar' );
my %hash = ( foo, 'Bar' );

The second should make it a little clearer why assigning a list to a
hash causes the behaviour you saw.

A little further demonstration in the Perl debugger:

  DB<1> @foo = qw(one two three four);
  DB<2> x \@foo;
0  ARRAY(0xa3cb200)
   0  'one'
   1  'two'
   2  'three'
   3  'four'
  DB<3> %hash = @foo;
  DB<4> x \%hash
0  HASH(0xa45a508)
   'one' => 'two'
   'three' => 'four'

Does that help make it clearer?

> and how template toolkit do the relation between database fiels and 
> values  with simple list ?
> for my point of view , a simple "complet" example will be good idea
> in documentation.
> complet = perl part AND ttk part

Well, the TT part is out of scope for D::P::D, as you could be using
the data for any purpose.

However, what you get is a list of hashrefs; iterating over that in TT
tends to look like e.g.:

[% FOR person IN people %]
    Hi [% person.name %]!
[% END %]

Where "people" is the param name which contains the list of people.

For a quick example having fetched it from a DB, e.g.:

  get '/people' => sub {
      my @people = database->quick_select('people', {});
      return template 'people', { people => \@people };

Hope that all helps make things clearer for you?

David Precious ("bigpresh") <davidp at preshweb.co.uk>
http://www.preshweb.co.uk/     www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan        www.preshweb.co.uk/github

More information about the dancer-users mailing list