Package: libtemplate-perl
Version: 2.20-1
Severity: normal

TT2 seems to not properly handle a single-element array-of-hashrefs.  Below
is a sample script (test.pl and test.tt2) and its actual output, and
expected output.

It appears that when looping through an array of hashrefs, if there is only
a single element in the array, the FOR loop treats the hashref as the
array.

So in my example code, test_sub(0) will return the following data structure:

( { a => 0 } )

and test_sub(5) will return the following:

( { a => 0 }, { a => 1 }, { a => 2 }, { a => 3 }, { a => 4 } )

TT2 handles test_sub(5) properly, but treats test_sub(0) as though it had
returned:

( a, 0 )

The same behavior is observed when returning either an array of arrayrefs or
an array of hashrefs.

test.pl:
#!/usr/bin/perl
use Template;

Template->new({
    VARIABLES => {
        test_sub => sub {
            my ( $num ) = @_;
            my @x;
            push @x, { a => $_ } for (0..$num);
            return @x;
        }
    }
})->process("test.tt2");


test.tt2:
[%
"<h1>Test with 1 element</h1>\n";
"Number of elements returned: " _ test_sub(0).size _ "<br>\n";
"The elements:<br>\n";
"<ol>\n";
FOR x IN test_sub(0);
    "<li>" _ x.a _ "</li>\n";
END;

"<h1>Test with 5 elements</h1>\n";
"Number of elements returned: " _ test_sub(5).size _ "<br>\n";
"The elements:<br>\n";
"<ol>\n";
FOR x IN test_sub(5);
    "<li>" _ x.a _ "</li>\n";
END;
%]

Actual output:
<h1>Test with 1 element</h1>
Number of elements returned: 1<br>
The elements:<br>
<ol>
<li></li>
<h1>Test with 5 elements</h1>
Number of elements returned: 6<br>
The elements:<br>
<ol>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>

Expected output:
<h1>Test with 1 element</h1>
Number of elements returned: 1<br>
The elements:<br>
<ol>
<li>0</li>
<h1>Test with 5 elements</h1>
Number of elements returned: 6<br>
The elements:<br>
<ol>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>



-- System Information:
Debian Release: lenny/sid
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.26-1-amd64 (SMP w/2 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages libtemplate-perl depends on:
ii  libappconfig-perl             1.56-2     Perl module for configuration file
ii  libc6                         2.7-13     GNU C Library: Shared libraries
ii  perl                          5.10.0-14  Larry Wall's Practical Extraction 
ii  perl-base [perlapi-5.10.0]    5.10.0-14  minimal Perl system

libtemplate-perl recommends no packages.

Versions of packages libtemplate-perl suggests:
pn  libtemplate-perl-doc          <none>     (no description available)
pn  libtemplate-plugin-gd-perl    <none>     (no description available)
pn  libtemplate-plugin-xml-perl   <none>     (no description available)

-- no debconf information



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to