Positron version 0.0.5 released

2013-09-09

The next version of Positron, version 0.0.5, has been released on CPAN. This version took a bit longer to upload than to write, for some reason. But here it is.

This version adds two constructs. The assignment operator "=" and the escaping constructs "~" and "{~}".

Assignment

The "=" construct is not really an "assignment", more an "aliasing" or a "local override". It works by augmenting the current environment (variable lookup space) with an identifier as a key and an evaluated expression as the value:

// environment: {'title': 'The title', 'subtitle': 'The subtitle'}

[1, ['= title subtitle', 2, '$title', 3], '$title', 5]

-> [1, [ 2, 'The subtitle', 3], 'The title', 5]

Note how the second $title was not replaced with 'The subtitle', since the assignment is local to the enclosing list structure. This is achieved internally by creating a new Environment with the old Environment as its parent and containing only the key title, which points to the result of the expression subtitle, namely the string 'The subtitle'.

One use of this feature is obviously to save typing and/or execution, by binding more complex expressions such as post.image.link or hash(title). The other use is is conjunction with the special expression variable "_", introduced in version 0.0.4. One can now wrap the entire current environment in a new top-level variable:

[ '= document _',
  '$document.title', '$document.subtitle'
]

Or one can "drill down" into a map by going the other way:

[ '= _ document',
  '$title', '$subtitle'
]

This is especially useful when including common sub-templates from independent files, as they may require a different environment.

Escapes

The second new construct is the "~" construct, and related to that the "{~}" text replacement construct. Like comments, these evaluate to nothing, but they are the very last to be evaluated. So they can be used to talk about the other constructs inside the template language:

[ '~$variable', 'include in strings as {{~}$variable}' ]

-> [' $variable', 'include in strings as {$variable}' ]

The "~" is concise, clear, used for this one purpose only, and even keeps braces nicely balanced.

Next up for Positron::DataTemplate: still hardening. Parse::RecDescent has its own ideas about error handling. It probably works exceedingly well for what it was made for, but not so good for embedding in a larger system. For starters, I'll need to redirect STDERR to some other filehandle (which wraps a scalar variable) just to get a hold of the errors…

Next up for Positron: the Positron::Template DOM-level templating engine, still.