-1

In Template Toolkit in Perl, I'm getting two errors instead of one.

local $SIG{__DIE__} = \&fatal;

print template('home.htssdfgsml');

sub template {
# --------------------------------------------------------------
#
  my ($file, $vars) = @_;
  my ($output, $template);

  my $config = {
      INCLUDE_PATH => $TEMPLATE_PATH
  };
  $template = Template->new($config);
  $template->process($file, $vars, \$output) || die Template->error;

  return $output;
}
sub fatal {
# --------------------------------------------------------------
#
  my $msg   = shift;

  print_header();

  print "<p><font face='Tahoma,Arial,Helvetica' size=2>A fatal error has occured:</font></p><blockquote><pre>$msg</pre></blockquote></font></p>\n";
}

and I get this output with two errors instead of one:

A fatal error has occured:

file error - home.htssssml: not found


A fatal error has occured:

file error - home.htssssml: not found at /var/path/path/file.pm line
29.

Any suggestions on how to just print one error?

Jonathan
  • 3,893
  • 5
  • 46
  • 77
  • 1
    Using your sample script and filling some values in, I cannot reproduce the problem. The script dies with an error: `file error - abcdef.html: not found`. (This is Template-Toolkit 2.24) – Slaven Rezic Sep 25 '13 at 05:54
  • I edited the post to give more information. Any suggestions on why I'm getting two errors? – Jonathan Sep 25 '13 at 20:07

1 Answers1

1

Okay this return if $^S apparently fixes it so that it only gives one error (including the line number)

sub fatal {
# --------------------------------------------------------------
#
  return if $^S;
  my $msg   = shift;

  print_header();

  print "<p><font face='Tahoma,Arial,Helvetica' size=2>A fatal error has occured:</font></p><blockquote><pre>$msg</pre></blockquote></font></p>\n";
}

This seems like bad code since it checks for a die in an eval. Does this seem adequate?

Jonathan
  • 3,893
  • 5
  • 46
  • 77
  • 1
    Checking the value of `$^S` is indeed the fix here. Generally, setting `$SIG{__DIE__}` may be dangerous. See also `perldoc perlvar`, especially the paragraph beginning with "Due to an implementation glitch..." – Slaven Rezic Sep 25 '13 at 22:26