What you are trying to do is very interesting, and it seems you are running into problems
because template-toolkit uses the Perl-ism of stuffing everything into a big hash.
I think the best way for you to approach this is to keep your hash in Perl-land, and create
functions, in Perl, to add things to it. You can then call these functions from Java using callbacks
and construct you hash. Here's what I mean:
package myTT;
# Your hash stays here my $H = {} ;
# Store a key, perhaps nested inside sub hashes. sub put { my $key =shift ; my $val= shift ;
my @path = split('.', $key) ; my $last = pop @path ; my $h = $H ; foreach my $p (@path){ $h = $h->{$p} ; } $h->{$last} = $val ; }
sub get { # Same logic... }
By adding an extra attribute to 'put' you'll be able to handle special
cases like function calls (i.e using "no strict 'refs'" and &{$val}) or whatever.
This way your data never leaves Perl and will be exactly the way template-toolkit
wants it.
Good luck,
Patrick ---------------------------------- | Patrick LeBoutillier | [EMAIL PROTECTED]
From: "Ludovic Dubost" <[EMAIL PROTECTED]> To: [EMAIL PROTECTED] Subject: Passing Hash values as Objects in Inline::Java Date: Mon, 06 Oct 2003 19:37:20 +0200
Hi,
I'm trying to use Inline::Java to use the Perl Template Toolkit (http://www.template-toolkit.org/)
from Java.. But I'm having little problems with communicating Hashtables to perl.
The way things work in the template toolkit is that you pass to templates a perl Hash that contains a lot of things..
It can contain functions, objects, hashes, arrays and strings..
What I would like to achieve is having some callbacks from the templates usage of all this to Java objects and functions.. For the moment I'm focusing on variables and objects:
Example template:
Version: [% VERSION %]<br> Param 1: [% param1 %]<br> Param 2: [% param2 %]<br> Param 3: [% param3 %]<br> Param 5: [% param5 %]<br> Param 5 name: [% param5.name %]<br> Param 5 value: [% param5.value %]<br> Param 4 name: [% param4.name %]<br> Param 4 value: [% param4.value %]<br> Function call: [% Param('toto') %] <br>
If I have a perl Hash:
VERSION -> $vars{'VERSION'} param1 -> $vars{'param1'} param2 -> $vars{'param2'} param3 -> $vars{'param3'} param5.name -> $vars{'param5'}->{'name'} param5.value -> $vars{'param5'}->{'value'} Function call -> $vars{'Param'} is a function
param5.name could also be handled by an AUTOLOAD function if param5 is a perl object.
Using Inline::Java I've seen two solutions to make it work:
Solution 1: Make the template toolkit understand seemlessly the Java objects
In this case the 'vars' variable would be a Hashtable.
So it would be:
VERSION -> vars.get('VERSION') param1 -> vars.get('param1') param5.name -> (Hashtable)vars.get('param1)).get('name')
I've managed to make the Inline::Java code call the 'get' or 'put' method when it doesn't find a member or a method..
I'm blocked because at one point the template toolkit wants to add a perl Hash to the vars Hashtable and Inline::Java does not want to convert such an object..
I'm thinking I would need to build a wrapper object in Java to store by Hash but I don't really know how to store by object, especially if I want to build a wrapper around the perl reference..
With this solution, I'm not sure how I can allow to call Java methods seemlessly.. Maybe just creating the perl wrapper will do it..
Solution 2: Allowing to manipulate perl hashes from Java
This solution is much safer at execution time as the data given to the template toolkit is much more close to what it gets in Perl.
However this can be heavy as it might be needed to copy a lot of data and make a lot of call to perl to fill objects..
By trying this I found the same problem with hashes.. I was trying to store my perl hash in Java and then call perl functions to add data to it, but this was refused by the Inline module as it could not convert the Hash to a java object..
Any toughts in this area ? Is there a chance to make this work ?
Ludovic
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=features/junkmail