Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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.

Cheers,
Ovid

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (7)
    As of 2014-10-20 04:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (72 votes), past polls