in reply to Re^8: displaying html file in the browser using perl
in thread displaying html file in the browser using perl

Without knowing what your HTML file contains, I cannot reproduce your problem. Please replace the reading of the HTML file into $html by hardcoding the value of $html. For example:

my $html = <<'HTML'; <html> <body> <!--#include file="somefolder/cc.html" --> </body> </html> HTML

See also SSCCE for an incredibly effective technique on how to debug things and provide information for others so that they can diagnose your situation and suggest approaches.

When I modify your script to be self-contained, it does just what I expect:

use strict; use warnings; my $htmlfile ='/var/www/html/xx/xxx/1204.html'; my $html = <<'HTML'; <html> <body> <!--#include file="somefolder/cc.html" --> </body> </html> HTML #path of the included file my $includepath="/var/www/html/xx/xxx/"; if ($html =~ s{#include file="([^"]+)"}{&add_template($1)}gei) { print "the file has include file"; print $html; } else { print "doesn't have"; print $html; } sub add_template { my $file = shift; print "$file\n"; my $display_file="$includepath$file"; print $display_file, "\n"; if (-e $display_file){ #print "$display_file"; return read_file($display_file); } else { # warn "$display_file not found"; return "<!-- $display_file not found to include -->"; } }

You will note that I changed the error message so that it outputs the value of $display_file. This is highly important as in your case, there may be a typo or a permissions error or something.

Replies are listed 'Best First'.
Re^10: displaying html file in the browser using perl
by tsdesai (Acolyte) on Dec 20, 2016 at 10:00 UTC
    Apologies, for not including the code for the html file. contents of 1204.html(the main file i am reading)
    <HTML> <HEAD> <LINK REL=StyleSheet TYPE="text/css" HREF="pages.css" TITLE=Pages><TIT +LE>New Academic </TITLE> </HEAD> <BODY BACKGROUND="/ff/img/bg2.gif" BGPROPERTIES="FIXED" BGCOLOR="#FFFF +FF"> <!-- Start of banner --> <CENTER> <!-- End of Banner --> <CENTER> <!-- Start of Topic Table --> <TABLE WIDTH="100%" BORDER=0 CELLSPACING=10 CELLPADDING=10> <TR> <!-- Start of Left Column --> <TD VALIGN=TOP> <TABLE WIDTH=100 BORDER=0 CELLSPACING=0 CELLPADDING=0> <!--#include file="somefolder/cc.html" --> </TABLE> </TD> <!-- End of Left Column --> <!-- Start of Middle Column --> <!-- Start of Topic --> <TD WIDTH="100%" CLASS=topic VALIGN=TOP> <H2><FONT CLASS=cf>Announcements</FONT></H3> <H3>New Academic</H3> fffff test some test content <P><FONT SIZE=-1 COLOR="#000099"> </TD> <!-- End of Middle Column --> <!-- End of Topic --> <!-- Start Right Column --> <TD WIDTH=100 VALIGN=TOP> </TABLE> </TD> <!-- End of Right Column --> </TR> </TABLE> <!-- End of Topic Table --> </BODY> </html>
    my perl code as test.p is as below
    use strict; use warnings; use File::Slurp qw/read_file/; use CGI; print CGI::header(); my $htmlfile ='/var/www/html/xx/xxx/1204.html'; my $html =read_file($htmlfile); my $includepath="/var/www/html/xx/xxxx/somefolder/"; if ($html =~ s{#include file="([^"]+)"}{&add_template($1)}gei) { print "the file has include file"; print $html; } else { print "doesn't have"; print $html; } sub add_template{ my $file = shift; #print "$file"; my $display_file="$includepath$file"; # print $display_file; if (-e $display_file){ #print "$display_file"; $sfile= read_file($display_file); #tried to see if it pri +nts file and it does print print $sfile;# displaying the included file return read_file($display_file); } else { # warn "$display_file not found"; return '<!-- $display_file not found to include -->'; } }
    I hope i have now covered everything that i have been using . I have tried to display the included file in the subroutine by adding the sfile and it does seem to print but not when i do return read file. I hope i am being clear. Really appreciate all your help. Many Thanks, T

      I showed you a good approach how you can inline your HTML into your program and eliminate some more moving parts from it. Why did you ignore that?

      Is the problem in reading the original HTML file? Does the program with the HTML inlined as I showed behave differently?

      Is all of the HTML necessary or does a smaller part suffice?

      Does reading the include file work? How does it fail?

      I note that you reverted the interpolation of $display_file when you output your error message:

      return '<!-- $display_file not found to include -->';

      Why did you remove that interpolation?

      When I inline your HTML with the program I posted, the relevant output I get is:

      <TABLE WIDTH=100 BORDER=0 CELLSPACING=0 CELLPADDING=0> <!--<!-- /var/www/html/xx/xxx/somefolder/cc.html not found to incl +ude --> --> </TABLE>

      Which is what I expect. What is the HTML source output you get on your machine?

      </c>
      FINALLY, I HAVE GOT IT WORKING ....... I HAD TO CHANGE MY REGULAR EXPRESSION AND THEN IT STARTED WORKING MAGICALLY. Thank you everyone. Many Thanks, T
        :)