Hello I've got stange problem with dancer with twitter boostrap I use use Dancer::Plugin::Database; my $dbh = database({driver => 'mysql', database => 'asav'}); my %allCommandeBNP = database->quick_select('CommandeBNP', {} ); my @allCommandeBNP= %allCommandeBNP; my $allCommandeBNP=@allCommandeBNP; In my mysql table has 36 items and $allCommandeBNP egal 36 I use if (params->{cmd} && params->{cmd} eq 'list') { return template 'template/navbar_login' => { username => params->{username}, password => params->{password}, tab => 3, ccmd => \%allCommandeBNP, show_list => "Liste des N° de commande:", }; } and in my template [% FOREACH s IN ccmd.keys.sort %] [% ccmd.$s.Ckey %]- [% END %] the result is 18 items, one item on two and only even item Liste des N° de commande: 210- 208- 206- 204- 202- 200- 198- 196- 194- 192- 190- 188- 186- 182- 184- 212- 214- 216- On mysql.log with request log activated , request is SELECT * FROM `CommandeBNP` if someone has a idea ?
On 04/08/2013 07:44 PM, Hugues Max wrote:
Hello I've got stange problem with dancer with twitter boostrap I use
use Dancer::Plugin::Database; my $dbh = database({driver => 'mysql', database => 'asav'}); my %allCommandeBNP = database->quick_select('CommandeBNP', {} );
my @allCommandeBNP= %allCommandeBNP; my $allCommandeBNP=@allCommandeBNP;
In my mysql table has 36 items and $allCommandeBNP egal 36
I use
if (params->{cmd} && params->{cmd} eq 'list') { return template 'template/navbar_login' => { username => params->{username}, password => params->{password}, tab => 3, ccmd => \%allCommandeBNP, show_list => "Liste des N° de commande:", }; }
and in my template
[% FOREACH s IN ccmd.keys.sort %] [% ccmd.$s.Ckey %]- [% END %]
the result is 18 items, one item on two and only even item
Liste des N° de commande: 210- 208- 206- 204- 202- 200- 198- 196- 194- 192- 190- 188- 186- 182- 184- 212- 214- 216-
On mysql.log with request log activated , request is SELECT * FROM `CommandeBNP`
if someone has a idea ?
You assign the database result to a hash instead of an array, so 18 records end up as hash keys and 18 records as hash values. Regards Racke -- LinuXia Systems => http://www.linuxia.de/ Expert Interchange Consulting and System Administration ICDEVGROUP => http://www.icdevgroup.org/ Interchange Development Team
On Mon, 08 Apr 2013 19:44:24 +0200 Hugues Max <huguesmax@gmail.com> wrote:
Hello I've got stange problem with dancer with twitter boostrap I use
use Dancer::Plugin::Database; my $dbh = database({driver => 'mysql', database => 'asav'}); my %allCommandeBNP = database->quick_select('CommandeBNP', {} );
The docs[0] say of quick_select: returns either the first matching row as a hashref if called in scalar context, or a list of matching rows as hashrefs if called in list context You're assigning that to a hash; that implies list context, so your hash will have both keys and values representing rows. e.g. you'll end up with a hash looking like: ( 'HASH(0x5356)' => \%row1, 'HASH(0x5367)' => \%row2, ) etc. You want to assign straight to a list: my @allCommandeBNP = database->quick_select('CommandeBNP', {} ); Then pass that straight to your template, e.g. ccmd => \@allCommandeBNP, And in your template, just iterate over that. [0] https://metacpan.org/module/Dancer::Plugin::Database::Handle -- David Precious ("bigpresh") <davidp@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
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 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 bye Hugues Le 08/04/2013 21:25, David Precious a écrit :
On Mon, 08 Apr 2013 19:44:24 +0200 Hugues Max <huguesmax@gmail.com> wrote:
Hello I've got stange problem with dancer with twitter boostrap I use
use Dancer::Plugin::Database; my $dbh = database({driver => 'mysql', database => 'asav'}); my %allCommandeBNP = database->quick_select('CommandeBNP', {} ); The docs[0] say of quick_select:
returns either the first matching row as a hashref if called in scalar context, or a list of matching rows as hashrefs if called in list context
You're assigning that to a hash; that implies list context, so your hash will have both keys and values representing rows.
e.g. you'll end up with a hash looking like:
( 'HASH(0x5356)' => \%row1, 'HASH(0x5367)' => \%row2, )
etc.
You want to assign straight to a list:
my @allCommandeBNP = database->quick_select('CommandeBNP', {} );
Then pass that straight to your template, e.g.
ccmd => \@allCommandeBNP,
And in your template, just iterate over that.
[0] https://metacpan.org/module/Dancer::Plugin::Database::Handle
On Mon, 08 Apr 2013 21:39:45 +0200 Hugues Max <huguesmax@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@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
yes, thanks, this is clearer now I'm going to sleep with aspirin.. :-) good night Hugues Le 08/04/2013 21:50, David Precious a écrit :
On Mon, 08 Apr 2013 21:39:45 +0200 Hugues Max <huguesmax@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?
And the best one (a revelation for me, at least) is Dancer's DSL get '/' => sub { .. }; is really my $code = sub { .. }; get('/', $code); But the former is more elegant and expressive. On Apr 8, 2013, at 12:57 PM, Hugues Max <huguesmax@gmail.com> wrote:
yes, thanks, this is clearer now I'm going to sleep with aspirin.. :-) good night Hugues
Le 08/04/2013 21:50, David Precious a écrit :
On Mon, 08 Apr 2013 21:39:45 +0200 Hugues Max <huguesmax@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?
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Mon, 8 Apr 2013 20:50:11 +0100 David Precious <davidp@preshweb.co.uk> wrote:
So, the following two are equivalent:
my %hash = ( foo => 'Bar' ); my %hash = ( foo, 'Bar' );
Actually, they're not, the second is a syntax error as I didn't add quotes around foo. I of course meant: my %hash = ( foo => 'Bar' ); my %hash = ( 'foo', 'Bar' ); :) -- David Precious ("bigpresh") <davidp@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
participants (4)
-
David Precious -
Hugues Max -
Puneet Kishor -
Stefan Hornburg (Racke)