in reply to displaying html file in the browser using perl

I know a few template languages, but can not recognize that syntax. What you can do is use regular expressions:

So you have aa.html which contains:

<html><body> <p>before</p> <include file ="hh.html"> <p>after</p> </body></html>

Then a cgi that reads this file and:

#!/usr/bin/perl use CGI; #my $q = CGI->new; print CGI::header(); my $file_to_read = 'aa.html'; my $html =read_file($file_to_read); $html=~s/<\s*include\s+file\s*=\s*"([^"]+)"\s*>/&add_template($1)/gexi +; print $html; sub add_template{ use File::Slurp qw/read_file/; return read_file($_[0]); }

The important part is the regexp, that basically is:

$html =~ s/<include file="([^"]+)">/&add_template($1)/gexi

It will find the include file template syntax, and replace the found expression with the content of the file...

note: You need to add exception stuff like:

if(!-f $_[0]){ return "Could not include template '$_[0]' because: NO FILE" }elsif(!-r $_[0]){ return "Could not include template '$_[0]' because: NO ACCESS TO FILE +"; }

Also writing "print CGI::header();" is bad form, try to stick to styles like:

cgi_script@perlmeme.org

Replies are listed 'Best First'.
Re^2: displaying html file in the browser using perl
by tsdesai (Acolyte) on Dec 17, 2016 at 12:24 UTC
    Thank you so much . Its not that there will always be an include file so i will need to exception rules and print the file accordingly. I have tried your code and unfortunately it does the same thing prints out the file which is probably aa.html but not the include file . Below is how my html file looks that i am trying to read with perl
    <HTML> <body> <!--#include file="somefolder/cc.html" --> </body> </html>

      Hi tsdesai,

      That looks like Server-Side Includes, and it would have been better if you had shown this in the original post. A quick search on CPAN reveals CGI::SSI, perhaps that module can help you.

      Regards,
      -- Hauke D

        yes, it server side includes in the html page. I will have to go through it. Not sure how much success, i am going to get.
Re^2: displaying html file in the browser using perl
by tsdesai (Acolyte) on Dec 19, 2016 at 11:42 UTC
    Thank you, I had some luck with this not quite to to the point when it displays the page, but at least it gives an error No file .But the file is definitely there. Below is my updated code
    my $includepath="somefolder/somefolder/"; $html =~ s/<!--#include file="([^"]+)"-->/&add_template($includepath.$ +1)/gexi; print $html; sub add_template{ #use File::Slurp qw/read_file/; if(!-f $1){ return "Could not include template '$_[0]' because: NO F +ILE" }elsif(!-r $1){ return "Could not include template '$_[0]' because: NO AC +CESS TO FILE +"; } else { return read_file($_[0]); } }
    I am not quite sure what i am doing wrong but there's something that i am missing definitely. Many Thanks, T

      Try without the x modifier on the regex

      #!/usr/bin/perl use strict; use CGI; use File::Slurp qw/read_file/; my $includepath = "c:/temp/"; my $file_to_read = 'aa.html'; my $html = read_file($file_to_read); $html =~ s{<!--#include file="([^"]+)"-->} {&add_template($includepath.$1)}gei ; print CGI::header(); print $html; sub add_template{ my $file = shift; if (-e $file){ return read_file($file); } else { warn "$file not found"; return '<!-- $file not found to include -->'; } }
      poj
        Hiya, I have tried your code. But i guess the problem is i have also comments in the page like <!-- start of the page -->. it tries to think start of the page is the file and fails . so something which specifically looks for include file rather than looking for comments in the page. I still think my reg expression is not right.At the moment its
        $html =~ s{<#include file="([^"]+)"}{&add_template($1)}gei;
        I would really appreciate any help on this . Many Thanks,

      You're testing $1, but your error message contains $_[0] as the filename. Which one is it you want?

      Let me suggest that you output the file name in your error message. If you're really intent on (re-)using $1, then you should also pass $1 to your subroutine read_file.

      Personally, I wouldn't reuse $1 outside of the subroutine where it is set through a match:

      my $includepath="somefolder/somefolder/"; $html =~ s/<!--#include file="([^"]+)"-->/&add_template($includepath.$ +1)/gexi; print $html; sub add_template{ my( $filename ) = @_; if(!-f $filename){ return "Could not include template '$filename' because: N +O FILE" }elsif(!-r $1){ return "Could not include template '$filename' because: N +O ACCESS TO FILE"; }else { return read_file($filename); } }

      Also consider why you're testin the existence and readability yourself? Personally, I would let read_file fail and output the error message from it instead of doing manual error checking.

        Yes you are right, i meant $1.I had changed my code. Now, I can include the file but think i need to change my regular expression. At the moment the contents of $1 is #include file="includes/sites.html" --> . I just need to get the the filename rather than entire thing and hence it keeps on coming back as no file found. Thanks, I really appreciate all your help.