The following code is part of a CGI.pm and HTML::Template script I'm working on.   It receives and untaints browser-supplied CGI param, then compares with directory listing of URLlist files, and serves content accordingly.   Amazingly enough, it actually works.   {grin}

Two questions:
1 - How to add site home (index.pl?page=home) as the default if *no* param given, but still fail if *bad* param given?
2 - Untainting regexp that will reject if non-word character anywhere in provided param, not just if non-word char is first character.

All suggestions welcome and appreciated.
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

 

# Read in URL file query and untaint # one or more word characters if ($query = param('page') =~ /(\w+)/) { $urlist = $1; } else {die "Please request pages by alphanumeric name only. You might find what you're looking for by starting at site home of http://host.dom/index.pl?page=home\n"; } # Build array of urlist files # Confirm that supplied param is valid file opendir DIR, "$confdir/"; my @files = grep { $_ ne '.' && $_ ne '..' && } readdir DIR; closedir DIR; unless (grep{$_ eq $urlist} @files) { die "You requested a page that does not exist. You might find what you're looking for by starting at site home of http://host.dom/index.pl?page=home\n"; } # read lists of page URLs from external file # loop through lists, parsing for HTML::Template use unless (my $return = do "$confdir/$urlist") { die "Cannot parse $urlist: $@" if $@; die "Cannot do $urlist: $!" unless defined $return; die "Cannot run $urlist" unless $return; } for (my $i = 0; $i < $#url_array; $i+=2) { my($loop, $aref) = @url_array[$i, $i+1]; my @vars; for (my $j = 0; $j < $#{$aref}; $j+=2) { my($name, $url) = @{$aref}[$j, $j+1]; push @vars, { name => $name, url => $url }; } $template->param($loop, [ @vars ]); }

In reply to Default CGI.pm param() if none provided? by ybiC

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.