Perl Template Toolkit
Accessing Hyphenated Hashtable Keys
Question
Can you access hash keys that have hyphenated names?
For example, if you declare the following hashref in Perl:
$hashref = {
'regularKey' => 1,
'hyphenated-key' => 1,
};
And then try to access the entries via the Template Toolkit:
1. [% hashref.regularKey %] # Okay
2. [% hashref.hyphenated-key %] # Generates error
The error reads:
Argument "" isn't numeric in subtraction (-) at myTemplatizedFile line 2.
Is there a way to get around the toolkit's assumption that the hyphens are for subtraction?
What I Tried
I tried escaping the hyphens with backslashes, but it generates this error when processing the template:
error - parse error - myTemplatizedFile line 138: unexpected token (\)
Backup Plan
I see two options to work around the issue:
- Change the keys to remove the hyphens.
- Create another hashref through which to access the hyphenated names, which repackages the names without hyphens. E.g.:
Add in Perl:
$intermediateHashref = {
'unhyphenatedKey' => $hashref->{'hyphenated-key'},
}
Change templated file:
1. [% hashref.regularKey %] # Okay
2. [% intermediateHashref.unhyphenatedKey %] # Okay