Hi, I have almost finished a web script to dump a load of data taken from my website, to a file. I just want some inteligent monks to check it over, and possibly provide an alternative solution to the 'if' statments i have in the 'Check For Bad Input Section'.
#!/usr/bin/perl -T use strict; use warnings; use CGI; ######################## # Set System Variables # ######################## my $w = new CGI; my $redir = new CGI; my $ip = $ENV{REMOTE_ADDR} || 'N/A'; my @errors = (); my $file = time.int rand 1_000_000; ###################### # Set Variable Specs # ###################### my %spec = ( fullname => qr/^ [-.\w\s] {3,30} \z/x, username => qr/^ [A-Za-z] [-.\w]{2,16} \z/x, mothers => qr/^ [-.\w\s] {3,30} \z/x, email => qr/^ [\w\.-]+ \@ (?:[a-z\d-]+\.)+ [a-z\d]+ \z/ix, conditions => qr/^Yes\z/, ); my %fields; ####################### # Check For Bad Input # ####################### foreach( keys %spec ){ my $v = $w->param($_) or do { push @errors => "$_ is not defined.\n"; next }; $v =~ $spec{$_} or do { push @errors => "$_ contains invalid input.\n"; next }; $fields{$_}=$v } if (getpwnam $fields{username}) { push @errors => "username already taken.\n"}; if ($fields{username} =~ m/^administrator|accounts|support|postmaster| +webmaster|spam-admin| |technical|billing|sales|misuse|mail|virus-admin|manager|usenet|ho +stmaster$/) { push @errors => "username already taken.\n"}; print_error( @errors ) if @errors; ################################### # Log User Details For Processing # ################################### open LOGFILE,'>>',"/var/log/tmp/$file" or die "$file opening failed: $ +!"; print LOGFILE join(',',@fields{qw/fullname username mothers email/},$i +p),"\n"; close LOGFILE; ########################## # Display Completed Page # ########################## print $redir->redirect('http://www.foo.com'); ###################### # Display Any Errors # ###################### sub print_error { print $w->header('text/plain'), @_; exit; }

edited: Tue Jul 8 18:36:58 2003 by jeffa - (title change: was Almost Perfect Code?)


In reply to Code review: validation regexes by Anonymous Monk

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.