Perl
  Home arrow Perl arrow Page 3 - Perl 101: The email form
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl 101: The email form
By: Pete Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2002-04-25


    Table of Contents:
  • Perl 101: The email form
  • Hello world in Perl
  • Refining Your Script
  • More Regular Expressions
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101: The email form - Refining Your Script
    ( Page 3 of 5 )

    So far we have a working script, but it lacks many of the refinements which distinguish amateur scripting from professional scripting; and more importantly, it is insecure - if your web server is cracked because of one of your CGI scripts then your provider will not be very happy.

    The golden rule in CGI programming is to never trust user input. Some users are stupid, others are malicious - just because you ask them for their name, it doesn’t guarantee that they won't enter a series of commands which *could* cause your web server to display it's password files.

    Characters such as '|`>< have special meanings to perl and can be used by the ingenious cracker to make your script do things it shouldn't. In fact, it's much safer to list what we *should* allow rather than what we should *disallow*. In this particular case, limiting user input to letters, numbers, underscores, spaces, periods, question marks, exclamation marks, hyphens, and at signs (@) should be sufficient.

    Here's where the fun starts. We're going to be using regular expressions, which are a very important (and often complex) part of Perl. If you are coming from another programming language you may already be familiar with regular expressions (or regex's, as the are called for short). If this is your first time then they can look very daunting, however fear not as all will be explained.

    First off, a simple one:

    unless ($name =~ /^[\w ]/)
    {
    print "Oops you entered your name incorrectly - please go back and check it<br>";
    die;
    }


    The important part of this is $name =~ /^[\w .]/ .Here we are testing to see if the name supplied by the user contains any characters which are not letters, numbers, or spaces. The two backslashes are boundaries – everything inside of them is treated by Perl as a regex, so our regex is:

    ^[\w ]

    \w is a shorthand meaning 'any word character', however Perl's definition of a word character is not what might you expect: to Perl it means 'any letter, number or an underscore'. The blank space following the \w is literal – i.e. Perl will look for a blank space.

    The square brackets ([ and ]) indicate that everything inside them is an alternative: Perl will be happy if it finds either a word character *or* a space. Finally, we invert the meaning of [\w ] by putting a carot (^) at the beginning:

    unless ($name =~ /^[\w ]/)
    {
    print "Oops you entered your name incorrectly - please go back and check it<br>";
    die;
    }


    Now that you have a basic understanding of regular expressions, this line could be rewritten in English as:

    If $name contains any character which is NOT a letter, number, underscore or space, THEN print an error message and stop.

    So that was your first lesson in pattern matching and the bizarre world of regular expressions. If you understand everything we've covered then give yourself a pat on the back - it can be very hard at first, but soon you'll be able to write your own regex's with your eyes shut. If you are still unsure of regular expressions, then please take the time to re-read this page and suss them out - the effort will pay off once you start writing your own Perl scripts.

     
     
    >>> More Perl Articles          >>> More By Pete Smith
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek