Well, heh I'm still a newbie and I just thought I'd post something I've done recently and maybe get some comments on it heh (please don't be to insulting lol).
#!/usr/bin/perl print "Welcome To Easy Storage V0.5\n"; until ($op =~/Q/i) { print "Would you like to (R)etrive stored data, (A)dd more information + or (Q)uit?\n"; $op=<STDIN>; if($op =~ /A/i) { print "Enter Name as EXPLZ and number as 1 to stop entering ne +w number\n"; until ($name =~ /EXPLZ/) { print "\nName: "; chomp ($name=<STDIN>); print "\nNumber: "; chomp ($num=<STDIN>); print "\nE-Mail: "; chomp ($email=<STDIN>); open FILEHANDLENAME, ">>storage.txt"; print FILEHANDLENAME $name," ",$num,"$email","\n"; }; }; if ($op =~ /R/i) {print "Please Enter The Name of The Person whoms inf +romation you want\n"; print "Name: "; chomp ($name2=<>); open FILE, "<storage.txt" or die; $line=<FILE>; while ( my $line = <FILE> ) { if ($line =~/$name2/i) {print $line,"\n"}; }; }; };
Any comments?

Replies are listed 'Best First'.
Re: Phone Number Storage
by blakem (Monsignor) on Jul 03, 2001 at 11:13 UTC
    Just two quick comments....
    First:
    open FILEHANDLENAME, ">>storage.txt"; print FILEHANDLENAME $name," ",$num,"$email","\n";
    would probably be better written as:
    $datafile = '/full/path/to/storage.txt'; #somewhere near the top ... open FILEHANDLENAME, ">>$datafile" or die "Couldn't open $datafile for + appending: $!"; print FILEHANDLENAME "$name $num $email\n";
    The first line helps us keep certain things configurable, and will probably help us down the line when we want to modify the script slightly. The second line gives us some feedback if our file handle fails (you used this construct later in the script.... its valuable here too) The third line uses interpolation and is much easier to read than the original.

    Second:

    $line=<FILE>; while ( my $line = <FILE> ) {
    What is that first line for? Are you purposefully throwing away the first line in the file? It could be that the first line in the datafile is for human consumption (i.e.
    ---------- BEGIN storage.txt ------------ NAME EMAIL PHONE Joe Blow joe@joe.com 123-1233 Jon Blow jon@jon.com 345-3455 ---------- END storage.txt ------------
    In that case a comment is warranted, to let everyone else know why that first line is being tossed.

    All in all, it looks pretty good.

    -Blake

Re: Phone Number Storage
by Brovnik (Hermit) on Jul 03, 2001 at 14:11 UTC
    It would be better to store the data in a format which allows you to parse it in later in the same program or in a future version.

    As a start, use ',' as a separator, rather than ' ', otherwise it is difficult to know where the name ends and the email starts. E.G.

    Frank Wright frank@lloyds.com 212-04567-98 Frank Lloyd Wright frank@wright.com 212-04567-98
    (Yes, you could write a whizzy regex to anchor the email, but a separator is easier).
    print FILEHANDLENAME join(',',$name,$num,$email),"\n";

    This would allow you later to search only the name, by splitting the input as in comes in. Otherwise you are also matching on the email address which may not be what you want. (In my eaxmple above, a search for 'lloyd' would have matched both lines.

    use strict; # This should go at the top of the file use warnings; # So should this, to catch errors. open FILE, "<storage.txt" or die $@; # report error on failure while (<FILE>) { my ($name,$email,$number) = split(/,/,$_); if ($name =~ /$name2/i) { print "$_\n"; } }
    Things to think about later :
    • What happens if the name added contains a ',' ? (Hint: consider using Text::CSV)
    • How about adding command line access, e.g. easy -R Frank (Hint: consider Getopt::Std)
    • If you are going to add more options later, (such as the ability to delete or change a name), how would you make the code maintainable ? (Hint: consider putting the actions each into their own subroutine)

    --
    Brovnik
Re: Phone Number Storage
by gornox_zx (Priest) on Jul 03, 2001 at 17:48 UTC
    Have you thought of making it a CGI so that you could access your information over the web? Instead of having the interactive inputs, you could make a MySQL database to store it, and have forms that read, store, and search the database. This would make it easier to search and parse your information than it would be using the flat file.
    For example you could do:

    select * from phone_list where name="Lloyd"

    or

    select * from phone_list where number=$number
Re: Phone Number Storage
by John M. Dlugosz (Monsignor) on Jul 04, 2001 at 01:48 UTC
    I'd say that's a pretty good script for learning, and much better written than your other one. I don't think these belong in CUFP, though: try Seekers of Perl Wisdom with a title asking for feedback.

    Keep plugging away at it, though.

    —John