On Tue, 19 Aug 2003 [EMAIL PROTECTED] wrote:

> Assuming I have a hash ref $foo passed to TT2 as foo, why would foo.size
> return undef? And foo is not empty. I foreach through it immediately
> after... Any scalar returns a 1, which I understand is because scalars
> are cast as lists for list virtual methods.
>  
> I originally ran into this issue while trying to find a reasonably
> intelligent way to deal with nested hashes with foreach(es). some of the
> values are scalar and need to be dealt with as such, but I'm really
> looking for a way to test if the value is a ref and handle it
> appropriately if it is.
>  
> Basically, I want the following data structure to be printed out:
> $foo = {
>           key => value ,
>           key => value ,
>           key => {
>                    key => value ,
>                    key => value ,
>           } ,
>           key => value ,
> };
>  
> But at this point I'd settle for an explanation as to why size doesn't
> seem to be behaving.

Mark,

You don't have enough code here for us to know for sure what's
happening, but if you're passing $foo as above to the process
method, then you won't have anything called foo in your template to
work with.  You'll just have all the 'key' variables.  That could
explain why foo.size is undef.

Here's a short script that passes your $foo (slightly modified) to a
template as foo and then prints the size, iterates over the
key/value pairs of foo (though without handling the nested hash
issue) and then printing out a Data::Dumper dump of foo (which may
or may not suit your purposes:

----------------------------------------------------------------------
#!/usr/bin/perl

use strict;
use Template;

my $foo = {
        key1 => 'value1' ,
        key2 => 'value2' ,
        key3 => {
                key31 => 'value31' ,
                key32 => 'value32' ,
        } ,
        key4 => 'value4' ,
};

my $tt2 = new Template();
$tt2->process(\*DATA, { foo => $foo });

__DATA__

the size of foo is [% foo.size %]

iterating through the keys of foo:
[%- FOREACH k = foo.keys.sort %]
 [% k %] = [% foo.$k %]
[%- END %]

[%- USE Dumper %]
dumping contents of foo:
[% Dumper.dump(foo) %]
----------------------------------------------------------------------

The output I get from this script is:


the size of foo is 4

iterating through the keys of foo:
 key1 = value1
 key2 = value2
 key3 = HASH(0x811415c)
 key4 = value4

dumping contents of foo:
$VAR1 = {
          'key2' => 'value2',
          'key4' => 'value4',
          'key1' => 'value1',
          'key3' => {
                      'key32' => 'value32',
                      'key31' => 'value31'
                    }
        };


I hope that helps.

Dave

/L\_/E\_/A\_/R\_/N\_/T\_/E\_/A\_/C\_/H\_/L\_/E\_/A\_/R\_/N\
Dave Cash                              Power to the People!
Frolicking in Fields of Garlic               Right On-Line!
[EMAIL PROTECTED]                                  Dig it all.


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to