Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

HTML::Template and filehandle ?

by gctaylor1 (Hermit)
on Jul 21, 2009 at 15:18 UTC ( #781989=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I've got 10-20 text files that I want to include in one HTML file so a user would see one big scrollable set of data in the browser(and be able to use the browser search feature). As far as I can tell there isn't an HTML method/tag to include external text files like this. I was thinking about using Perl to directly manipulate the HTML file but that will be messy and error prone because not only would I need to append the text files in to one big HTML file but might have to remove sections as well. The text files are generated by another process and there's no guarantee of the file being present, the size, or content being predictable.

I've looked at HTML::Pager and HTML::Template and have been trying to get the latter working without success to open a filehandle and then display the contents. I'll be using CGI too because I want to get user input.

Am I even close or is there a better way to do this? c:\perl\bin\perl.exe use warnings; use strict; use CGI; use HTML::Template; my $template = HTML::Template->new(filename => 'c:\inetpub\wwwroot\cgi +-bin\template2.tmpl'); open(FH, 'c:\temp\output.txt') || die("Can't open output.txt: $!"); my $ioref = \*FH; $template->param(FILE => "$ioref"); $template->param(PATH => 'yellow brick road'); print "Content-Type: text/html\n\n", $template->output
#template2.tmpl <html> <head><title>Test Template</title> <body> Contents of file are <TMPL_VAR NAME=FILE> <p> Path is set to <TMPL_VAR NAME=PATH> </body> </html>
This doesn't work, and instead prints
Contents of file are GLOB(0x152ab4c) Path is set to yellow brick road
Thank-you for your time.

Replies are listed 'Best First'.
Re: HTML::Template and filehandle ?
by davorg (Chancellor) on Jul 21, 2009 at 15:25 UTC
    As far as I can tell there isn't an HTML method/tag to include external text files like this.

    Server side includes, perhaps?

    You are creating a HTML::Template variable called FILE. And that contains the contents of the Perl variable $ioref. And that contains a filehandle. So you shouldn't really be surprised if you get the filehandle printed out instead of the contents of the file. If you want the contents of the file, then you'll need to read the file.

    $template->param(FILE => do { local $/; <FH> });

    See the Copyright notice on my home node.

    Perl training courses

      Thanks daveorg!

      That works as advertised. It doesn't look like I want it to look but that's a topic for another node.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://781989]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-07-04 00:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found