CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Check My Perl Code

 



the1stefan
New User

Dec 20, 2006, 11:44 AM

Post #1 of 3 (366 views)
Check My Perl Code Can't Post

Can any please check my Perl Code for the following:

I have to create a Perl program to acquire customer information, the program must ask for the following: Customer Reference Number (4 Digits Only), Title (Between 1 and 4 Alpherabtic Characters, the allowed list is Dr, Lady, Lord, Miss, Mr, Mrs, Ms, Sir), Surname (Between 1 and 20 Alpherabtic Characters), Forname (Between 1 and 20 Alpherabic Characters), Sex (1 Character M or F upper or lower case), Date of Birth (in format DD/MM/YYYY), Vision Measurement (1 or 2 digits between 1 and 99). All the inputs have to be verified, if any verification fails then a suitable error message must be displayed and another prompt to re-enter the same item again. Then lastly the program needs to ask have you another customer details to enter, if yes to start again or exit if no.

I know, that the code is a bit long winded but is there a shorter way around:



$condition = 0;

do{

print("Please enter a four digit customer reference. ");

chomp($customer_reference = <>);



if($customer_reference =~ /^[0-9]{4}$/){

print("\nThank you. Your customer reference is: $customer_reference\n");

$condition = 1;

}

else{print("\nFOUR DIGITS PLEASE!.\n");

}

}while($condition == 0);



$condition = 0;

do{

print ("\nWhat is your title? ");

$title = <STDIN>;

if ($title =~ /^[DLMS][adiorsy]{1,3}$/){

print("\nThank you.\n");

$condition = 1;

}

else{print("\nTry again.\nThe allowable list of titles is:\nDr, Lady, Lord, Miss, Mr, Mrs, Ms, Sir.\nFormat is Xxxx.\n");}

}while($condition == 0);



$condition = 0;

do{

print ("\nWhat is your surname? ");

$surname = <STDIN>;

if ($surname =~ /^[A-Z][a-z]{1,19}$/){

print("\nThank you.\n");

$condition = 1;

}

else{print("\nTry again.\nFormat is Xxxx.\n");}

}while($condition == 0);



$condition = 0;

do{

print ("\nWhat is your forename? ");

$forename = <STDIN>;

if ($forename =~ /^[A-Z][a-z]{0,19}$/){

print("\nThank you.\n");

$condition = 1;

}

else{print("\nTry again.\nFormat is Xxxx.\n");}

}while($condition == 0);



$condition = 0;

do{

print ("\nWhat is your sex? ");

$sex = <STDIN>;

if ($sex =~ /^[MmFf]{1}$/){

print("\nThank you.\n");

$condition = 1;

}

else{print("\nTry again.\nFormat is M or F.\n");}

}while($condition == 0);



$condition = 0;

do{

print("\nPlease enter your date of birth. ");

$date_of_birth = <STDIN>;

if ($date_of_birth =~ /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/){

print("\nThank you.\n");

$condition = 1;

}

else{print("\nTry again.\nFormat is DD/MM/YYYY.\n");}

}while($condition == 0);



$condition = 0;

do{

print("\nPlease enter your vision measurement. ");

$vision_measurement = <STDIN>;

if ($vision_measurement =~ /^[0-9]{1,2}$/){

print("Thank you.\n");

$condition = 1;

}

else{print("ONE or TWO DIGITS ONLY!!\n");}

}while($condition == 0);



KevinR
Veteran


Dec 20, 2006, 3:04 PM

Post #2 of 3 (361 views)
Re: [the1stefan] Check My Perl Code [In reply to] Can't Post

Your code looks like beginner code as expected. If you are out of the beginner phase then I would say your code needs improving, but of you are still in the early stages of learning perl and it's various operators and functions and regexps and etc, then the code is OK.

You should start using "strict" and "warnings" though in all your perl scripts:


Code
#!/usr/bin/perl 
use strict;
use warnings;


those are the two best tools you have for learning perl. Some pointers for writing perl code:

http://perldoc.perl.org/perlstyle.html

** use the code tags for posting code on this forum. :)
-------------------------------------------------


stupeas
Novice

Dec 21, 2006, 2:49 AM

Post #3 of 3 (359 views)
Re: [the1stefan] Check My Perl Code [In reply to] Can't Post

Well Done!! I told you that you could do it. Youve also got a couple os shortcuts I missed!!

It works fine.

I left you a PM by the way.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives