diff options
author | Dave Arter <davea@mysociety.org> | 2020-01-10 12:33:15 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2020-01-10 12:33:15 +0000 |
commit | 9bc7db869aba7d6321c89d0a92d9ece8b2b6a21c (patch) | |
tree | 44f4d535abeba0cbb49a766fe814f0c639b8f4f8 | |
parent | 6508407642869999c1659b5eb3ffbf71895151ea (diff) |
Cope with swapped operands in template operators
Perl may pass swapped operands to ensure the first one is always an object
that implements the relevant operator.
-rw-r--r-- | perllib/FixMyStreet/Template/Variable.pm | 8 | ||||
-rw-r--r-- | t/template.t | 6 |
2 files changed, 11 insertions, 3 deletions
diff --git a/perllib/FixMyStreet/Template/Variable.pm b/perllib/FixMyStreet/Template/Variable.pm index 9b5a0fcc4..0142a4db5 100644 --- a/perllib/FixMyStreet/Template/Variable.pm +++ b/perllib/FixMyStreet/Template/Variable.pm @@ -8,13 +8,15 @@ sub op_factory { my ($op) = @_; return eval q|sub { - my ($self, $str) = @_; + my ($self, $str, $swapped) = @_; if ( ref $str eq __PACKAGE__) { - return $self->{value} | . $op . q| $str->{value}; + return $self->{value} | . $op . q| $str->{value} unless $swapped; + return $str->{value} | . $op . q| $self->{value}; } else { - return $self->{value} | . $op . q| $str; + return $self->{value} | . $op . q| $str unless $swapped; + return $str | . $op . q| $self->{value}; } }|; } diff --git a/t/template.t b/t/template.t index 1763a7f12..91e4ccc84 100644 --- a/t/template.t +++ b/t/template.t @@ -30,4 +30,10 @@ $tt->process(\'H: [% s.split(":").join(",") %]', { }, \$output); is $output, 'H: 1,sp<i>l</i>it,3'; +$output = ''; +$tt->process(\'[% size %] [% 100 / size %] [% size / 100 %]', { + size => 4 +}, \$output); +is $output, '4 25 0.04'; + done_testing; |