Why does not die $ template-> error () show the 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 print line number and new line? My conclusion is as follows:

 ~ 502 $ perl template.pl
 file error - non-existent-file: not found ~ 503 $ 
+3
source share
2 answers

Templatereturns an object of type error Template::Exception. The object has an overloaded line, which is used when printing a value, but when it dielooks at a value, it sees a link and does not add a line number or a new line. Force the value to 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.
+10
source

@Eric OPs, .

, , , perl ( ). . :

use Template;
my $template = Template->new();
# Clearly a division by zero bug
$template->process(\"[% 1 / 0 %]")
    or die $template->error();

:

undef error - Illegal division by zero at input text line 1.

. perl. :

my $template = Template->new();
$template->process(\"[% 1 / 0 %]")
    or die $template->error() . ' ';

:

undef error - Illegal division by zero at input text line 1.
  at test.pl line 11.

, perl. . ( , ...)

:

use Template;
my $template = Template->new();
$template->process(\"[% 1 / 0 %]")
    or do {
        my $error = $template->error . '';
        chomp $error;
        die $error;
    };

:

undef error - Illegal division by zero at input text line 1. at t2.pl line 15.

.. :

sub templateError {
    my ($template) = @_;
    my $string = $template->error->as_string;
    chomp $string;
    $string =~ s/(line \d+)\.$/$1/;
    return $string;
}
...
use Template;
my $template = Template->new ();
$template->process (\"[% 1 / 0 %]")
    or die templateError($template);

, :

undef error - Illegal division by zero at input text line 1 at test.pl line 30.

OP:

file error - non-existent-file: not found at test.pl line 31.
0

Source: https://habr.com/ru/post/1745386/


All Articles