in reply to Not my first program, but the first I'll share...

I decided to spend a little time in sprucing this up a small bit, just for fun. I took out the nasty javascript, changed the copyright (not that this snippet is copyrighten or anything), threw in a templating system, and some very simple error checking. Enjoy.

#!c:/perl/bin/perl -w $|++; use strict; use CGI::Simple; use HTML::Template; my $q = CGI::Simple->new; my $t = HTML::Template->new( filehandle => *DATA ); my @ext = qw( pl css htm html shtm shtml ); my $file = $q->param('file'); my $fh; if ( ($file =~ /[^a-zA-Z0-9_\-\.]/) or ($file =~ /\.\./) or ($file eq '.') or !(grep { $file =~ /\.$_\z/i } @ext) !(-e $file) or !(open $fh, '<', $file) or ) { $t->param( title => 'My File Viewer', file => 'Script Error', code => 'An error occured while preparing the file for display.' ); } else { $t->param( title => 'My File Viewer', file => $file, code => do { local $/; <$fh> } ); } print $q->header, $t->output; exit; __DATA__ <html> <head> <title>Pekkhum's Script Veiwer</title> </head> <body> <p style="text-align: center; font-weight: bold"> <!-- TMPL_VAR NAME="title" ESCAPE="HTML" --> </p> <p><hr /></p> <p style="text-decoration: underline; font-weight: bold"> <!-- TMPL_VAR NAME="file" ESCAPE="HTML" --> </p> <form> <textarea name="code" rows="30" cols="90" readonly> <!-- TMPL_VAR NAME="code" ESCAPE="HTML" --> </textarea> </form> <p> <small>&copy; Copyright 2003 Nathan Bessette</small> </p> </body> </html>


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Replies are listed 'Best First'.
Re: Re: Not my first program, but the first I'll share...
by pekkhum (Sexton) on Oct 28, 2003 at 00:23 UTC
    Heh, thanks for all this new information Coruscate... Now this aught to take me a while to decode... Sadly such programming goes beyond the scope of The Visual Quickstart Guide I'm learning from. ^_^

    Not sure what's up, but there when I try running your version of the script I get:
    Can't locate HTML/Template.pm in @INC (@INC contains: e:/ActivePerl/Perl/lib e:/ActivePerl/Perl/site/lib .) at c:\webweaver\docs\sv.pl line 6. BEGIN failed--compilation aborted at c:\webweaver\docs\sv.pl line 6.

    I found the HTML directory, and sure enough there is no module named Template. On my way to download it now.
      pekkhum, if you are using the Elizabeth Castro book, like I did, you will find that you will have to unlearn some stuff to conform with methods in the monastery. I recommend you switch to the O'Reilly books, Learning Perl (Llama book), Programming Perl (Camel book), and CGI Programming in Perl. Invaluable.

      Speaking of invaluable: HTML::Template! BTW, the module is not called Template, but HTML::Template.

      Appreciate your bravery in posting your code, I've learned a lot by following the thread. You don't get better teachers than the likes of davido and Coruscate.

        Thanks for the suggestion, I've been saving up for those books for a while, but with my mainboard blown, I'm gonna be to broke, for a long time. ^_^ The Castro book is a great first step, but now I am indeed trying to learn as much as I can from the monastary. I need all the help I can get. Anyone here is welcome to message me on my AIM name, pekkhum. (Only AIM for now, since my private computer is dead.

        Also, I can't compile the HTML::Template module due to the sadly limited scope of my (Free) Perl interpreter, I need to find one compiled for ActivePerl 5.8.0 on Windows XP. Anyone know a site that has it?

      I was asked by pekkhum if I could explain a bit, so here's the best job I am capable of doing. Hopefully it's enough to help out a bit.