diff --git a/lib/Template/Document.pm b/lib/Template/Document.pm index 5f829a94..afe0b164 100644 --- a/lib/Template/Document.pm +++ b/lib/Template/Document.pm @@ -25,6 +25,7 @@ use strict; use warnings; use base 'Template::Base'; use Template::Constants; +use Template::Exception; our $VERSION = '3.100'; our $DEBUG = 0 unless defined $DEBUG; @@ -205,8 +206,14 @@ sub AUTOLOAD { $method =~ s/.*:://; return if $method eq 'DESTROY'; -# my ($pkg, $file, $line) = caller(); -# print STDERR "called $self->AUTOLOAD($method) from $file line $line\n"; + + # metadata items are read-only; throw an error if called with arguments + # (which indicates an assignment attempt via the stash) + if (@_) { + die Template::Exception->new('file', + "template metadata item '$method' is read-only"); + } + return $self->{ $method }; } diff --git a/t/document.t b/t/document.t index 586522bb..7725fb37 100644 --- a/t/document.t +++ b/t/document.t @@ -143,6 +143,25 @@ title: My Template Title -- expect -- some output +-- test -- +# GH #270: assigning to template metadata should throw, not silently fail +[% META foo = 'bar' -%] +[% TRY -%] +[% template.foo = 'blech' -%] +ERROR: no exception raised +[% CATCH file -%] +OK: [% error.info %] +[% END %] +-- expect -- +OK: template metadata item 'foo' is read-only + +-- test -- +# reading metadata still works after the readonly check +[% META title = 'Read Test' version = 42 -%] +title=[% template.title %] version=[% template.version %] +-- expect -- +title=Read Test version=42 + -- stop -- # test for component.caller and component.callers patch -- test --