[Templates] Problem with merge() and object methods

[prev] [thread] [next] [Date index for 2004/06/10]

From: Chris Warren
Subject: [Templates] Problem with merge() and object methods
Date: 12:40 on 10 Jun 2004
[I couldn't find a mention of this in the list archives; my apologies if
 I'm asking a redundant question. Also, apologies for the length of the
 e-mail.]

It seems like the merge() virtual method fails when you try to merge
single-item lists returned by object methods. I'll include my example
code, which I hope should explain what I'm seeing.

#### A class with get_* methods available.
package TestClass;
use strict;
use warnings;

sub new {
    my $class = shift;
    bless {
	foo => [qw/ one /],
	bar => [qw/ uno /],
    }, $class;
}
sub get_foo {
    my $self = shift;
    return @{$self->{foo}};
}
sub get_bar {
    my $self = shift;
    return @{$self->{bar}};
}
1;
####

#### My test program.
#!/usr/local/bin/perl
use strict;
use warnings;
use Template;
use TestClass;

my $tmpl = Template->new;
my $test = TestClass->new;
my $vars = {
    object => $test,
    alpha  => [ 'charlie' ],
    bravo  => [ 'delta' ],
};

$tmpl->process('template.txt', $vars) or die $tmpl->error;
####

#### My test template
[% allnums = object.get_foo.merge(object.get_bar) -%]
allnums value: [% allnums.join(', ') %]
get_foo value: [% object.get_foo.join(', ') %]
get_bar value: [% object.get_bar.join(', ') %]

[% callsigns = alpha.merge(bravo) -%]
callsigns value: [% callsigns.join(', ') %]
alpha     value: [% alpha.join(', ') %]
bravo     value: [% bravo.join(', ') %]
####

#### My output when I run the test program
allnums value: one
get_foo value: one
get_bar value: uno

callsigns value: charlie, delta
alpha     value: charlie
bravo     value: delta
####

I would expect the "allnums" value in the filled-out template to be
"one, uno". If I add another element to one of the lists I'm trying to
merge, it works as I'd expect:

    bless {
	foo => [qw/ one /],
	bar => [qw/ uno dos /],
    }, $class;

    allnums value: one, uno, dos

Is there something I'm doing that's causing one of the one-element lists
to be thrown away? Is there anything I can do to make this work the way
I'd expect it? Or have I got a mistaken understanding of the way merge()
works?

Thank you.

csw
        -- 
         /¯\
 \ /   ASCII RIBBON CAMPAIGN
  X  Against HTML Mail and News
 / \

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] Problem with merge() and object methods
Chris Warren 12:40 on 10 Jun 2004

Generated at 08:55 on 15 Mar 2005 by mariachi v0.52