Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


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

In reply to "Correct" program style questions by Ovid

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (9)
    As of 2018-05-23 17:06 GMT
    Find Nodes?
      Voting Booth?