I've put together a test script demonstrating how my data model works. Please tell me this fails for you, or I might go insane. Bonus points if you can tell me how to fix it.
#!/usr/bin/perl use strict; use warnings; use Template; my $container = Container->new(); $container->add( Object->new( name => 'foo', value => 10, usefulness => 0 ) ); $container->add( Object->new( name => 'bar', value => 20, meaning => 42 ) ); my $vars = { 'container' => $container }; my $template = <<EOT; container=[% container %] all: [% FOREACH element IN container.all.sort('name') %] [% element %] name is [% element.name %] [% END %] one: [% FOREACH element IN container.one(10).sort('name') %] [% element %] name is [% element.name %] [% END %] EOT my $tt = Template->new(); $tt->process(\$template, $vars); package Object; sub new { my $class = shift; my $self = {@_}; return bless($self, $class); } package Container; sub new { my $class = shift; my $self = {@_}; return bless($self, $class); } sub add { my $self = shift; my $new = shift; push @{$self->{'objects'}}, $new; } sub all { my $self = shift; return @{$self->{'objects'}}; } sub one { my $self = shift; my $filter = shift; return grep { $_->{'value'} == $filter } @{$self->{'objects'}}; }
Time flies when you don't know what you're doing
In reply to Re^2: Template, sorting an array with only one member
by FloydATC
in thread Template, sorting an array with only one member
by FloydATC
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |