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

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

Replies are listed 'Best First'.
Re^3: displaying html file in the browser using perl
by poj (Abbot) on Dec 19, 2016 at 12:10 UTC

    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,
        #$html =~ s{<#include file="([^"]+)"}{&add_template($1)}gei; # remove < ^ here $html =~ s{#include file="([^"]+)"}{&add_template($1)}gei;
        poj
Re^3: displaying html file in the browser using perl
by Corion (Patriarch) on Dec 19, 2016 at 11:51 UTC

    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.

        My tip is to never use $1 (and its other numbered siblings) in a subroutine that doesn't also set them up. Always pass them around as parameters and use the parameters in the subroutine only. My example shows that approach.

        This is IMO the better approach because it avoids the problem that a subroutine itself might execute another match (maybe for debugging) and then will overwrite the value you wanted to use.