in reply to Peruse my code...

Well, now all input is checked, but there are caveats about these checks. If some input fields are not given, you'll get warnings for undefined values. Also some Regexps are broken or inefficient. All in all, i'd write it like this:

#!/usr/bin/perl -T use strict; use warnings; use CGI; my $w = new CGI; my $ip = $ENV{REMOTE_ADDR} || 'N/A'; my @errors = (); my $file = time.int rand 1_000_000; my %spec = ( username => qr#^[A-Za-z][-.\w]{2,29}$#, domain => qr#^(a|c|f|h|ho|i|k|o|s|x)\.com$#, email => qr#^[\w\.\-]+\@(?:[a-z\d\-]+\.)+[a-z\d]+$#i, service => qr#^Yes|No$#, conditions => qr#^Yes$#, ); my %fields; foreach( keys %spec ){ my $v = $w->param($_) or do { push @errors => "$_ not defined"; next }; $v =~ $spec{$_} or do { push @errors => "$_ invalid input"; next }; $fields{$_}=$v } print_error( @errors ) if @errors; open LOGFILE,'>>',"var/log/accounts/$file" or die "$file opening failed: $!"; print LOGFILE join(',',@fields{qw/username domain email service/},$ip) +,"\n"; close LOGFILE; print $w->header('text/plain'), "done"; sub print_error { print $w->header('text/plain'), @_; exit; }
--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: Peruse my code...
by Arien (Pilgrim) on Aug 31, 2002 at 17:33 UTC

    Also some Regexps are broken or inefficient.

    my %spec = ( username => qr#^[A-Za-z][-.\w]{2,29}$#, domain => qr#^(a|c|f|h|ho|i|k|o|s|x)\.com$#, email => qr#^[\w\.\-]+\@(?:[a-z\d\-]+\.)+[a-z\d]+$#i, service => qr#^Yes|No$#, conditions => qr#^Yes$#, );

    These regexes allow for a newline after the last character. This could lead to trouble, for example when assuming your log file consists of one line with fields seperated by commas (as one probably would).

    The regex for domains assumes all domains are .com, which they are not. Apart from that, it can be written in a more efficient way.

    Your service regex is flawed: it allows for any string as long as it starts with Yes or ends with No (and optional newline).

    Also, there's no need to escape a dash (like you do in the email regex) if you put it at the beginning or end of a character class.

    In summary, I'd write those as:

    my %spec = ( username => qr/^ [A-Za-z] [-.\w]{2,29} \z/x, domain => qr/ ^h\.net\z | ^(?:ho | [acfikosx])\.com)\z /x, email => qr/^ [\w\.-]+ \@ (?:[a-z\d-]+\.)+ [a-z\d]+ \z/ix, service => qr/^(?:Yes|No)\z/, conditions => qr/^Yes\z/, );

    — Arien