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.


In reply to Re: Not my first program, but the first I'll share... by Coruscate
in thread Not my first program, but the first I'll share... by pekkhum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.