if I did it in a more idiomatic way. Can any monks give me some suggestions to make it less awkward? Also, do I need to change anything if I want to run it under mod_perl?

The actual paths are weird because they are test files. And I'm running it on win32. Valid files and their short names are stored in the file that $links contains. This ensures that people can't try to node=../../../etc/passwd. The $is_script thing skips printing a header if the file contains java script, since most of that stuff would be in between the head tags.


Thanks,

derek3000

#!//Hebe/I$/perl/bin/perl -w # #index.pl #fetches content of link #and prints it out along with #consistent headers and footers. use strict; #like always use CGI; #duh use CGI::Carp; #now we can actually find error messages my $links = 'h:\perl scripts\links.txt'; my @lines; my $q = new CGI; my $node = $q->param('node'); my $is_script = $q->param('script') || 'no'; #list of valid filenames and handles(index) goes here my %known_files; my %top_level; populate_hash($links); #check to see if input is valid if(exists($known_files{$node})){ open(NODE, $known_files{$node}) or die "Can't open file. Valid input though. $!\n"; while(<NODE>){ push(@lines, $_); } close NODE; } else { die "Don't fool around with the url, punk.\n"; } #do the output if($is_script eq 'yes'){ middle(); bottom(); } else { top(); middle(); bottom(); } #Subs under here #-------------------------------------------------------- #------------------------------------------- #gets hash keys and paths for valid files, #also populates second hash for menu sub populate_hash{ my $linkfile = shift; open(LINKS, $linkfile) or die "Can't get file for fresh links! $!\n"; while(<LINKS>){ (my $KEY, my $VALUE) = split /=/; $known_files{$KEY} = $VALUE; if($KEY =~ /announcements|links|news|info/i){ $top_level{$KEY} = $VALUE; } } close LINKS; } #------------------------------------------- #prints html header along with standard menu, #and soon a graphical header. sub top{ print $q->header('text/html'); print $q->start_html(-title=>$node, -author=>'schmidtd@co.delaware.pa.us', -BGCOLOR=>'white'); menu(); } #------------------------------------------ #this will probably stay the same. #it prints out the page-specific content. sub middle{ foreach(@lines){ print $_; } } #------------------------------------------ #prints out anything we want on the bottom, #such as a redundant navigation scheme, #or a link to the disclaimer, or both, #when we get it. sub bottom{ print $q->end_html; } #------------------------------------------ #prints out menu from hash of files. # sub menu{ print "|"; foreach(sort keys %top_level){ print $q->a( {href=>"h:\\perl scripts\\index.pl?node=$_"}, "$_"); print "|"; } }

In reply to I know this code could be better... by derek3000

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.