Search code examples
regexperltemplate-toolkit

Perl Template::Toolkit regex matching either values


I am trying to match either of the string in the Perl Template::Toolkit module.

url is getting its value from the script.

[% IF url == ("a/b/c" | "d/e/f") %]

Is this the right way of doing it? I looked at the documents. It mentioned the 'matches' method, but I am looking for a simpler way of doing it.


Solution

  • I think it's a little awkward to use regular expressions in Template::Toolkit, but in this case you can just write

    [% IF url == 'a/b/c' or url == 'd/e/f' %]
    

    If you need anything more complex then you're probably misunderstanding the rôle of templates, but you can always evaluate a Boolean condition within Perl and pass that value into the template


    Update

    Or you could use SWITCH, like this

    [% SWITCH url %]
    [%   CASE [ 'a/b/c', 'd/e/f' ] %]
           ...
    [% END %]