mexnix has asked for the wisdom of the Perl Monks concerning the following question:

In kind of an atempt to get me to program something worth something, I came upon the idea of making an address book. (hey, maybe someday a "little black book!" ha!) Although as of now, I haven't nailed down the interface side, I have some ideas:

That can all be handled later. My question deals with the data part of the program. I'm pretty sure that I want to put the data in a text file (I don't know THAT many women!), read the text file, and store it in the program as a hash of a hash using peoples nicknames (helps to keep it unique) as the keys . i.e.:

%addys = (
     reallyhotchick => {
                 phone=>'555.555.5555', 
                 name=>'Jane Shmoe', 
                 cell=>'555.555.5555', 
                 nick=>'reallyhotchick'}...)

What would be the best way to go about this? (field delimitation, how to get the data into the variables, etc) Is this a stupid way to go about this? I've tried a few things, but I always screw them up somehow. This is really a learning project for me. I will of course post the code upon finishing, but I don't expect to be life changing. If you think there's a better way (leaving out DB's to keep it simple) I open to suggestions. Thanks for your time.

Replies are listed 'Best First'.
Re: writing an address book program
by andreychek (Parson) on Jun 07, 2001 at 00:48 UTC
    Data::Dumper and MLDBM would work quite well. However, this would
    also be an excellent opportunity to make use of some XML.

    In an XML file, you could simply say:
    <addys> <reallyhotchick> <phone>555.555.5555</phone> <name>Jane Shmoe</name> <cell>555.555.5555</cell> <nick>reallyhotchick</nick> </reallyhotchick> </addys>
    There are quite a few Perl modules at CPAN
    which will parse an XML file for you. You can just go
    there and do a search for 'XML'. I have in the past found
    XML::Simple, XML::TokeParser, and XML::Twig to be useful.
    Those three all use XML::Parser to do their dirty work.

    If you're feeling particularly adventurous, you could always
    try and use XML::Parser directly. However, most of what you may
    want to do has already been implemented, and no need to
    reinvent the wheel unless you're trying to learn how
    something works :-)

    Good Luck!
    -Eric
      I hadn't thought of XML. I've actually been meaning to take a serious look at XML too. Thanks a bunch! Now I if I could only learn through osmosis...

      __________________________________________________
      %mexnix = (email = > "mexnix@hotmail.com", website => "http://mexnix.perlmonk.org");

        Osmosis.... MMmmmmmm... :-)

        Yeah, I actually picked up XML not too long ago myself.
        With the method you mentioned you are looking to use, XML::Simple
        might be your best bet.. you just point it to a XML
        file, and it will slurp the thing in and make it a
        hash of hashes (...of hashes of hashes, etc).

        However, just think how big this thing might get.. slurping the whole
        address book into a hash is loads of fun, but you might regret it later ;-)
        But you have to start somewhere, and XML::Simple is.. well.. simple :-) Have fun,
        -Eric

        There's an XML tutorial I found useful at w3schools, it's not Perl-related but a good intro to the world of XML.

        Update: After a comment by Beatnik I realised I should have included a link directly to the XML tutorial: here it is

        --
        Kevin O'Rourke
Re: writing an address book program
by damian1301 (Curate) on Jun 07, 2001 at 01:09 UTC
    Make a separate program to write to the text files (hey, Perl is great, right?) delimitered by some special character (~,;;,^, etc.).

    In your address book program, make it read from the file that you've already wrote to with the other program and split the lines on that special character.

    this is a quick hack:

    #!/usr/bin/perl -wT use strict; use Data::Dumper; my $del = '~'; my $file="file.txt"; my %hash; open(DB,"$file") || die "$!"; while(<DB>){ my ($nick,$phone,$name,$cell) = split/$del/; #split on $del. %_ = (nick=>"$nick",phone=>"$phone",name=>"$name",cell=>"$cell"); $hash{$nick}={%_}; } print Data::Dumper->new([\%hash],[qw(hash)])->Indent(2)->Quotekeys(0)- +>Dump; # the above is from the Data::Dumper tutorial on this site.


    This works for me and I am not aware of anyother way to do it. I will post the program to write to the file in a little bit. Hope i helped, goodbye

    UPDATE: Here is the program to write to the database file.

    #!/usr/bin/perl -wT use strict; use CGI qw/param textfield start_form end_form Tr Td table h2 submit/; my $file ="file.txt"; my $del ='~'; my $nick = param('nick'); my $phone= param('phone'); my $name = param('name'); my $cell = param("cell"); #----------- ...I love character classes :) ---- stuff(); if ($nick && $phone && $name && $cell){ $nick =~ s/[^A-Za-z_.]//; $phone =~ s/[^0-9\-]//g; $cell =~ s/[^0-9\-]//g; open(DB,">$file") || die "$! DEAD!"; select DB; print $nick,$del,$phone,$del,$cell; # this or the alternative # print "$nick$del$phone$del$cell"; confusing eh? select STDOUT; print "All done :-)" && exit; close DB; }else{print"<BR><BR>\n Nothing has been submitted to put in the file, +yet."; } sub stuff{ print start_form; print h2({-align=>'center'},"Add to the Bitch Book :)"); print table({-width=>'300'},Tr(Td( "Nickname: "),Td(textfield(-name=>'nick', -override=>1),"\n<BR>")) +, Tr(Td( "Phone Number: "), Td(textfield(-name=>"phone", -override=>1),"\n< +BR>")), Tr(Td( "Name: "), Td(textfield(-name=>"name", -override=>1),"\n<BR>")), Tr(Td( "Cell phone number: "), Td(textfield(-name=>'cell', -override=>1)) +)), submit, end_form; }
    Yay! I am *such* a bored XP whore... :)

    Tiptoeing up to a Perl hacker.
    Dave AKA damian

Re: writing an address book program
by TGI (Parson) on Jun 07, 2001 at 01:09 UTC

    Why not use DBD::CSV? That way, when you run for President, you can upgrade to DBD::Oracle with minimal work. There's an example of this in the OReilly CGI Programming book.

    Update: BTW, if you're on a windows box, indigo perl comes with an apache server set up for running CGI (and mod_perl) locally, although you'll probably want to tweak the apache configuration to allow only local access.


    TGI says moo

Re: writing an address book program
by stephen (Priest) on Jun 07, 2001 at 00:33 UTC
Re: writing an address book program
by toma (Vicar) on Jun 08, 2001 at 10:12 UTC
    If you think that you might want to use hierarchical data, or if you want to add lots of features to the program in the future, I would second the recommendation for XML. If you to start with something even more simple, I recommend Data::Table, which has a really simple interface, works with CSV files and databases, supports sorting, math operations on a column, HTML rendering, etc.

    Here's a snippet to get you going:

    use strict; use Data::Table; my $t= Data::Table::fromCSV("phonebook.csv"); $t->sort('lastname', 1, 0); # Sort by col 'lastname', alphabetic, as +cending print $t->html; # Print out phone book as an HTML table.

    Where the file 'phonebook.csv' contains:
    lastname,firstname,phone,TZ,date_entered Wall,Larry,555-1212,PST,4/3/2001 Schwartz,Randal,555-1212,PST,4/1/2001

    It emits HTML for this table:

    lastnamefirstnamephoneTZdate_entered
    SchwartzRandal555-1212PST4/1/2001
    WallLarry555-1212PST4/3/2001

    I think you have chosen a very nice project for learning perl.

    It should work perfectly the first time! - toma