h4ckroot has asked for the wisdom of the Perl Monks concerning the following question:
i know i didnt use any pragmas here like strict. :D but anyway i wanted your notes and advices SO Please post your notes.. Thanks in advance..#!/usr/bin/perl -w open TEMPFH,"DB.txt" or die "Sorry Cant Open File...$!"; #reading the File For Getting the Last id To Support Auto increment.Th +ere is another way to do it later #using Tail . while(<TEMPFH>){ $_ =~/^(\d*).*$/; $ID=$1 if eof; } close TEMPFH; print "Welcome To My Simple Phone Book:)\n"; print "What Do You want me To Do\n"; while(1){ print "[L]ist,[A]dd,[D]elete,[Q]uit\n"; $userInput = <STDIN>; chomp $userInput; if (uc $userInput eq "L" or uc $userInput eq "LIST"){ open READFH,"DB.txt" or die "Sorry Cant Open the DataBase...$!"; for (<READFH>){ print; } close READFH; } elsif (uc $userInput eq "A" or uc $userInput eq "ADD"){ open WRITEFH,">>DB.txt" or die "Sorry Cant open The Database..$!"; while(<WRITEFH>){ print;} while(1){ print "Please enter the name\n"; print "Please Note that Only chars,numbers and underScore are aLlo +wed with max 30 char and min One Char \n"; chomp($name = <STDIN>); if ($name !~ /^\w{1,30}$/){ print "Sorry Invalid Name Try Again\n"; next; } else{last;} } while(1){ print "Please Enter The Number\n"; print "Please Note that Only Numbers are allowed With max 30 digit + and min 1 digit\n"; chomp($number=<STDIN>); if ($number !~ /^\d{1,30}$/){ print "Sorry Invalid Number..Try Again\n"; next; } else{last;} } $ID++; print WRITEFH "$ID:$name:$number\n"; print "Added Succefully\n"; close WRITEFH; } elsif(uc $userInput eq "Q" or uc $userInput eq "QUIT"){ print "Thank You,Keep Visiting :D\n"; last; } elsif(uc $userInput eq "D" or uc $userInput eq "DELETE"){ print "Please Enter the Name You Wana Delete\n"; chomp($userToDelete = <STDIN>); open DELETEFH,"DB.txt" or die "Sorry Couldnt Open DB...$!"; open TEMP,">temp.txt" or die "Sorry Couldnt Open DB...$!"; print "Successfully Deleted...\n"; $myid=1; while (<DELETEFH>){ if (/^\d:$userToDelete:\d*$/) { $ID--; next; } else{ $_ =~ /^\d*:(.*)$/; print TEMP "$myid:$1\n"; $myid++; } } close DELETEFH; close TEMP ; rename "temp.txt","DB.txt" ; $myid=1; } else{ print "Sorry Invalid Option...Try Again"; } print "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Phone Book
by jethro (Monsignor) on Jan 04, 2009 at 19:36 UTC | |
|
Re: Phone Book
by apl (Monsignor) on Jan 04, 2009 at 20:17 UTC | |
|
Re: Phone Book
by Herkum (Parson) on Jan 04, 2009 at 19:14 UTC | |
by Lawliet (Curate) on Jan 04, 2009 at 20:02 UTC | |
|
Re: Phone Book
by GrandFather (Saint) on Jan 04, 2009 at 23:06 UTC | |
by h4ckroot (Initiate) on Jan 05, 2009 at 10:48 UTC | |
|
Re: Phone Book
by Anonymous Monk on Jan 04, 2009 at 18:44 UTC | |
|
Re: Phone Book
by fmerges (Chaplain) on Jan 04, 2009 at 21:25 UTC | |
|
Phone Book After Modifications
by h4ckroot (Initiate) on Jan 05, 2009 at 14:29 UTC | |
by zentara (Cardinal) on Jan 05, 2009 at 15:26 UTC |