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

Thank you so much for your help. It seems to work they way it should and only looks for include file. But, still doesn't display the included file when i read aa.html on the browser. I have double checked the path and have tried to print only the included file and it does print the file individually. When i try to read with aa.html it just kind of ignores and doesn't print the included file. I have no idea what i am missing now. Many Thanks, T
  • Comment on Re^6: displaying html file in the browser using perl

Replies are listed 'Best First'.
Re^7: displaying html file in the browser using perl
by Corion (Patriarch) on Dec 20, 2016 at 08:49 UTC

    When "running from the browser", you need to use absolute path names. Instead of trying to open somefolder/cc.html, you will have to use /home/tsdesai/somefolder/cc.html for example.

      Thanks, I have absolute paths. Below is my entire code if it helps.
      use strict; use warnings; use File::Slurp qw/read_file/; use CGI; print CGI::header(); my $htmlfile ='/var/www/html/xx/xxx/1204.html'; #mainfile to read my $html =read_file($htmlfile); #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"; my $display_file="$includepath$file"; # print $display_file; 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 -->'; } }
      The above code prints the main file which is 1204.html and then also detects that there is an included file given in the if condition but just doesn't print it. Not sure why. Many Thanks, T

        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.

        much Ho,Ho,Ho!

        When i first saw this i was concerned but didnt say anything

        $html =~ s{#include file="([^"]+)"}{&add_template($1)}gei
        now remember that the htmlish command is
        <!--#include file="somefolder/cc.html" -->
        and think of what is to be replaced-from in that statement .... and then think about what is left behind. Your included text is now between <!-- -->, ie html comments!

        now im not great at REs, but let me suggest

        $html =~ s{\<\!\-\-#include file="([^"]+)".*?\-\-\>}{&add_template($1) +}gei
        ( i escape things a lot 'cuz im never sure ....)

        edit:added ? to ".*?\-\-\>, its not good to be greedy here