tsdesai has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am trying to read the contents of html file and display it on the browser. So far, it works as in it will display the contents of the html . But the problem is the html file in turn has the <include file ="hh.html"> which is not displayed. I am not sure how can i display the contents of this included html file . Below is my code so far.

use File::Slurp qw/read_file/; print CGI::header(); my $html =read_file('xx.html'); print $html;

I am banging my head on this.

I would really appreciate any help on this.

Many Thanks,
T

Replies are listed 'Best First'.
Re: displaying html file in the browser using perl
by FreeBeerReekingMonk (Deacon) on Dec 17, 2016 at 09:57 UTC
    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

      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

      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

        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.

Re: displaying html file in the browser using perl
by Marshall (Canon) on Dec 16, 2016 at 23:13 UTC
    I am not quite sure where the problem is. print $html; will just print the HTML, not display it in the browser which is a separate program from Perl. If you want something displayed in the browser program, then you have to launch the browser to display it.

    I just saved your post as an html file from my browser. On Windows to launch the browser with that file as input goes like below. I do not know on Unix.

    #!/usr/bin/perl use warnings; use strict; my $htmlfile_name = 'C:/Documents and Settings/Ira/My Documents/displa +ying html file in the browser using perl.htm'; system (1, "$htmlfile_name"); #launch in default browser # program keeps going after browser is launched # __END__ Comment: if you use backticks, `$htmlfile_name`, Perl program will not continue until browser is closed. Depends upon what you want to do.
Re: displaying html file in the browser using perl (not Perl)
by LanX (Saint) on Dec 16, 2016 at 21:28 UTC
    Welcome

    First please use code tags <code>code</code> around your code, to make it readable for us.

    Then I'm sorry but this <include file...> html code has not much to do with Perl.

    Probably PHP or a template engine I'm not aware of?

    What's the origin of your example?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: displaying html file in the browser using perl
by FreeBeerReekingMonk (Deacon) on Dec 17, 2016 at 12:12 UTC
    Alternatively,

    HTML::Template does have template expressions to include files, you would need to rename it to:

    <TMPL_INCLUDE NAME="filename.tmpl">

    for it to work

Re: displaying html file in the browser using perl
by Anonymous Monk on Dec 19, 2016 at 19:48 UTC
    Use HTML::Display
    use HTML::Display; use File::Slurp qw/read_file/; my $html =read_file('xx.html'); display($html);