Hi Guys i just made some modifications that you suggested me and here is the code again,Please any advices or improvements are more than welcomed.
#!/usr/bin/perl -w use strict; my $ID; my $userInput; 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"; chomp($userInput = <STDIN>); if (uc $userInput eq "L" or uc $userInput eq "LIST"){ &list($ID); } elsif (uc $userInput eq "A" or uc $userInput eq "ADD"){ &add($ID); } 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"){ &delete($ID); } else{ print "Sorry Invalid Option...Try Again"; } print "\n"; } ###################################################################### +################################################# sub add{ open WRITEFH,">>DB.txt" or die "Sorry Cant open The Database..$!"; my $name; my $number; 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; } } $_[0]++; print WRITEFH "$_[0]:$name:$number\n"; print "Added Succefully\n"; close WRITEFH; } sub delete{ my $currentID=$_[0]; my $userToDelete; my $myid=1; 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...$!"; while (<DELETEFH>){ if (/^\d*:$userToDelete:\d*$/i) { $_[0]--; next; } else{ $_ =~ /^\d*:(.*)$/i; print TEMP "$myid:$1\n"; $myid++; } } if($_[0] < $currentID){ print "Successfully Deleted user:$userToDelete\n"; } else{ print "Error..Deleting User Please Check the username Again..,\n"; } close DELETEFH; close TEMP ; rename "temp.txt","DB.txt" ; $myid=1; } sub list{ open READFH,"DB.txt" or die "Sorry Cant Open the DataBase...$!"; for (<READFH>){ print; } close READFH; }
Thanks in advance.

In reply to Phone Book After Modifications by h4ckroot
in thread Phone Book by h4ckroot

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!
  • 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, details, 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, summary, 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.