Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Persistant variables in TT2

by Molt (Chaplain)
on May 10, 2005 at 14:39 UTC ( #455592=perlquestion: print w/ replies, xml ) Need Help??
Molt has asked for the wisdom of the Perl Monks concerning the following question:

Something I'm developing at the moment has a need for a mini-language, something that's quick and easy to use. In order to avoid reinventing wheels I decided to use TT2 syntax if possible.

The one problem I'm having with this is that I need to be able to process multiple templates whilst retaining state, effectively running multiple scripts as if they were one large script. In order to test if TT2 would allow this I wrote the following code:

#!/usr/bin/perl use strict; use warnings; use Template; # Initialise the variable hash, and create us a TT2 obj. my $vars = { a => 0}; my $tt2 = Template->new(); # Very simple template, should increment TT2's 'a' variable. my $template = "[% SET a=a+1 %] A is now [% a %]\n"; # Loop through, see if we can keep the new value of a. for (1..5) { $tt2->process ( \$template, $vars ); }

If anyone can suggest any way to keep the value of the variables between each call of process() in the above code I'd be a very happy little monk.

Comment on Persistant variables in TT2
Download Code
Re: Persistant variables in TT2
by davidrw (Prior) on May 10, 2005 at 14:47 UTC
    instead of passing in the scalar 'a', pass in a hashref with your data, and the changes will persist:
    use Template; my $vars = { a => 0 }; my $tt2 = Template->new(); my $template = "[% SET data.a=data.a+1 %] A is now [% data.a %]\n"; $tt2->process( \$template, { data => $vars } ) for 1..5;
    Update: My guess, based just on the behavior seen in my post and the OP, is that TT passes the data by value. So in the OP, the 'a' that gets modified isn't the real 'a'. In my post, we're not modifying the value (address) of 'data', but are deferencing it and then changing values, so they appear to persist.

      Thanks for that, it does seem to work quite nicely. I was admittedly hoping for some way to make it 'just work' but I guess this will serve my purposes fine.

Re: Persistant variables in TT2
by Anonymous Monk on May 10, 2005 at 14:51 UTC
    perldoc Template::Manual::Variables
    Local and Global Variables
    Any simple variables that you create, or any changes you make to existing variables, will only persist while the template is being processed. The top-level variable hash is copied before processing begins and any changes to variables are made in this copy, leaving the original intact. The same thing happens when you INCLUDE another template.
    So you can't do that as-is, but you could subclass Template::Stash or pull other similar dirty tricks :)
Re: Persistant variables in TT2
by merlyn (Sage) on May 10, 2005 at 17:04 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://455592]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (14)
As of 2014-12-19 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (87 votes), past polls