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

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.

Replies are listed 'Best First'.
Re^4: displaying html file in the browser using perl
by tsdesai (Acolyte) on Dec 19, 2016 at 13:36 UTC
    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.

        ok, i have updated the code so it uses $filename. But iam still having problems getting the name of the file from the regular expression.It still gets the entire include filename="hh.html--> Will keep you updated how i get on. Thanks, for all your help so far.