Search code examples
perltemplate-toolkit

Why doesn't die $template->error() show a line number?


In the following short program:

 use Template;
 my $template = Template->new (INCLUDE_PATH => ".");
 $template->process ("non-existent-file")
      or die $template->error ();

why doesn't die produce a line number and newline? My output looks like this:

 ~ 502 $ perl template.pl
 file error - non-existent-file: not found ~ 503 $ 

Solution

  • Template is returning an error object of type Template::Exception. The object has overloaded stringification which applies when the value is printed, but when die looks at the value, it sees a reference and doesn't append the line number and newline. Force the value into a string earlier to fix the problem:

    use Template;
    my $template = Template->new (INCLUDE_PATH => ".");
    $template->process ("non-existent-file")
      or die '' . $template->error ();
    

    prints

    file error - non-existent-file: not found at scratchpad.pl line 25.