in reply to Critque my second script for me?
Not too shabby. Nice use of subroutines, stick with organizing your code thusly and you'll be way ahead of the game.
Couple of things
Your return statement in parse_form() doesn't do anything, because you've declared those variables in a higher scope. You can take it out and your script will work the same.
I didn't see a place in your script where you used the standard interface to CGI.pm. Am I missing something? You could probably ditch the :standard use.
Globals can make things hard to debug, and will cut down on your ability to reuse your code. The way I like to think about subroutines is that they are black boxes- you pass to it directly what it needs, and you get from it exactly what you need. Examples?
or,my($topic,$words,$pass,$name,$user)=parse_form($q); sub parse_form { my $q=shift; #get the value from @_ my $name= $q->param('name')|| "Sludge Factor"; $name =~ s/\s/_/g; my $words = $q->param('words'); $words =~ s/\r\n/<BR>/g; # don't think this will work like you exp +ect with *nix, #but that might not matter. my $topic= $q->param('topic'); my $user = $ENV{'REMOTE_ADDR'}; # or my $user=$q->remote_addr(); my $pass=$q->param('pass'); return [$topic,$words,$pass,$name,$user]; }
because you don't need chomps, CGI.pm will do it for you.sub parse_form { my $q=shift; #get the value from @_ my $name= $q->param('name')|| "Sludge Factor"; $name =~ s/\s/_/g; my $words = $q->param('words'); $words =~ s/\r\n/<BR>/g; # don't think this will work like you exp +ect with *nix, #but that might not matter. return [$q->param('topic'), $words, $q->param('pass'), $name, $q-> +remote_addr()]; }
Now this subroutine can standalone, because we know exactly what goes in by looking at the first few statements, and exactly what comes out by looking at the return. This will allow you to reuse it elsewhere by just cutting and copying it, and when you get into OOP you'll be ahead of the game.
A note- using CGI to parse forms is a little easier than you're making it. . .
Keep up using strict, start thinking about subroutines as black boxes, and you're doing well. Globals are fine for smaller scripts, but when you get into larger apps, you'll have maintenace problems ('where does $poo come from again?').
If you're going to be doing a lot of CGI programming, you should take some time to learn a templatting system like HTML::Template. Once you start using it, you'll wonder how you made web apps any other way. . .
-Any sufficiently advanced technology is
indistinguishable from doubletalk.
|
|---|