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.


In reply to Re^9: displaying html file in the browser using perl by Corion
in thread displaying html file in the browser using perl by tsdesai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.