0

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:

  1. Change the keys to remove the hyphens.
  2. 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
Community
  • 1
  • 1
Barry Jones
  • 1,329
  • 1
  • 9
  • 16

0 Answers0