Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/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?)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Code review: validation regexes
by Abigail-II (Bishop) on Jul 08, 2003 at 07:17 UTC | |
by Anonymous Monk on Jul 08, 2003 at 11:52 UTC | |
by Abigail-II (Bishop) on Jul 08, 2003 at 19:19 UTC | |
|
Re: Code review: validation regexes
by nysus (Parson) on Jul 08, 2003 at 18:11 UTC | |
by l2kashe (Deacon) on Jul 08, 2003 at 18:19 UTC | |
by nysus (Parson) on Jul 08, 2003 at 18:40 UTC | |
by l2kashe (Deacon) on Jul 09, 2003 at 05:07 UTC | |
by bigj (Monk) on Jul 09, 2003 at 07:00 UTC | |
|