mschout / perl-text-template

Expand template text with embedded Perl
13 stars 6 forks source link

Template code does not display in debugger running under mod_perl #21

Open jayrifkinsv opened 1 year ago

jayrifkinsv commented 1 year ago

When using recent versions of Text::Template in a mod_perl environment and running the perl debugger via Apache::DB, if you break inside a template, the current line of code in the template does not display in the debugger, nor do other lines if you try debugger commands such as v, l, or w. Additionally, the line numbering in the debugger is off by 2.

Both of these issues seem to be due to the inclusion of the comment $fi_lcomment in $fi_progtext. The offset in the line numbering is due to the 2 carriage returns in $fi_progtext. I don't know how including the comment causes the template code not to display, but if we revert that line to an earlier version without the comment, the display in the debugger works fine. Therefore, I suggest the following patch, which detects whether the debugger is running and includes or excludes the comment accordingly:

--- latest - template code does not display in debugger under mod_perl
+++ suggested version - template code does display in debugger under mod_perl
@@ -304,8 +304,12 @@
     } elsif ($fi_type eq 'PROG') {
       no strict;
       my $fi_lcomment = "#line $fi_lineno $fi_filename";
-      my $fi_progtext =
-        "package $fi_eval_package; $fi_prepend;\n$fi_lcomment\n$fi_text;";
+      # This allows the perl debugger to show lines of template code and the correct line number
+      my $fi_progtext
+        = $^P ? "package $fi_eval_package; $fi_prepend; $fi_text;"
+              : "package $fi_eval_package; $fi_prepend;\n$fi_lcomment\n$fi_text;"
+              ;
+
       my $fi_res;
       my $fi_eval_err = '';
       if ($fi_safe) {

This fix works well for the debugger, which already displays the current line and the filename, so no functionality is lost.

Thanks, Jay

mschout commented 1 year ago

Good catch thanks! I'll try to get it into a release soon.