http://qs1969.pair.com?node_id=175443


in reply to Complex Data Structures

I think the most complex data-structure I've ever created was for HTML::Template. The module has two main structures - an array of ops including variables, text, loops and conditionals (@stack) and a hash of parameter names to the corresponding variable or loop (%map). The trick is that both the map and the stack point to the same underlying storage. This allows me to do something like this to set the value for a variable:

${$map{var_name}} = "text for var_name";

Then when I get to the place where I want to use the variable on the stack I just do:

$output .= ${$stack[$i]};

Since the same scalar is referenced from both @stack and %map I can get access to them both quickly and with no copying required.

Now, setting up this structure is indeed a royal pain in the ass but the payoff in output speed makes it all worthwhile. (Of course, these days all the real speed demons have moved on to HTML::Template::JIT!)

-sam