http://www.perlmonks.org?node_id=207263
Ovid has asked for the wisdom of the Perl Monks concerning the following question:

Bit of an odd question, but figured I would toss this out to some people. I'm preparing some material for people who may be new to the CGI module and to Web programming in general and I realized that some of my "style" in programming might seem a bit odd to some. As I am already familiar with my style, it seems natural to me. However, I thought I should toss this out to my fellow monks for some feedback.

#!/usr/bin/perl -wT use strict; use CGI qw/:standard/; use HTML::Entities; my $_name = param('name') || ''; my $_color = param( 'color' ) || ''; my ( $name ) = $_name =~ /^([[:alpha:][:punct:][:space:]]+)$/; my ( $color ) = $_color =~ /^([[:alpha:]]+)$/; encode_entities( $name ); print header, <<"END_HTML"; <html> <head><title>Test page</title></head> <body> <p>Your name is "$name" and the color you listed was "$color"< +/p> </body> </html> END_HTML

First, you'll notice that I tend to use two different variables for grabbing parameter values. I prefer two as it's much less likely that one will accidentally read bad data into the untainted variable. Also, the following popular construct can generate a warning if the parameter is undefined:

my ($action) = param('action') =~ /^([[:alpha:]]+)$/;

As I don't like spurious warnings in my error log, it seems cleaner this way.

Also, I am using POSIX character classes. I feel that they are easier on the eyes, but many don't care for them. I could also switch to Unicode if you think that would be more general purpose or go back to the traditional regex characters if that is what the audience will be more familiar with.

The Web sites that I build are usually, but not always for US audiences. As a result, I haven't had to worry too much about Unicode and friends. Would the program be less "correct" if I switched [:alpha:] to [a-zA-Z]? Or would it be better if I used \p{IsAlpha}? What are the pros and cons of using traditional regex characters versus POSIX or Unicode?

Any other comments welcome.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.