First question - yes, it is possible - but, you have many
problems with this script:
- You are using CGI.pm, yet you are explicitly printing
the content header - use $q->header instead.
- You are also using multiple print statements to generate HTML - use CGI.pm's methods or a templating tool
such as HTML::Template instead.
- You are trying to access an array's element with the wrong syntax: @foo[$bar] should be $foo[$bar] fix the 40 or so occurrences in
your code and you will get better results.
- You are using subroutines, but you are not passing
variables to them, opting for globals where globals are not
needed.
I hate to say it, but you have so many errors that if i
were you, i would start over. What are your requirements?
Write these down and work from there. Design! :)
Here is an example that prints the contents of the
current directory and generates links to each of the
files. It only scans one level deep, for more check out
File::Find. Each link displays the contents of
that file as text - care is taken to make sure that the
user cannot view files outside of the directory. Be careful
with this - it is a loaded gun! And always, always, use
the -T (taint option) on CGI scripts. Also,
mod_proxy might just be a better solution (thanks for the link
grinder).
#!/usr/bin/perl -Tw
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
my $file;
if ($file = param('file')) {
# only allow files in this dir
if ($file =~ /[\/~]/ or $file =~ /^\./) {
print header;
die "access to $file not allowed"
}
unless (open(FH,$file)) {
print header;
die "$file does not exist";
}
print header(-type=>'text/plain');
print while <FH>;
}
else {
print header,start_html('Contents of current dir');
print ul(li([
map {
a({-href=>qq|@{[script_name]}?file=$_|},$_)
} <*>
]));
}
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.