Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

MySQL and perl? Or something else?

by Andrew_Levenson (Hermit)
on Mar 14, 2007 at 19:35 UTC ( #604877=perlquestion: print w/replies, xml ) Need Help??
Andrew_Levenson has asked for the wisdom of the Perl Monks concerning the following question:

I've started yet another project, inspired by an outside source (A Ruby tutorial...). They suggested writing an address book and accessing it, and I realized that this could be a great way to learn about hashes.
After a lot of scribbling of notes during spanish and photography (class), I realized that I may be going about it wrong: Sure, a hash of hashes of hashes looks (structurally) like the way I should go about making one, but with the hypothetical user constantly adding new fields within new fields within new fields, this could easily get out of hand (memory wise) (and I won't even get into the logical nightmares I faced when trying to use my limited knowledge to parse/save/recreate these {hash trees?} between sessions).

Wracking my brain for things i've seen on the interweb, MySQL popped up as a solution. I don't know the first thing about it, or why it would help me, so I figured i'd ask the experts here.

Is MySQL a good way to implement this? Can I use it and perl together? If so, where would be a good place to learn it?

Thanks in advanced.
C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}

Replies are listed 'Best First'.
Re: MySQL and perl? Or something else?
by GrandFather (Sage) on Mar 14, 2007 at 19:54 UTC

    Databases in general and Perl go well together with modules like DBI and DBIx. The nice thing is that those paper over many of the differences between different (SQL accessible) databases by allowing pluggable back ends. There are a large range in the DBD name space.

    As for an address book. What is it if not a database?

    Take a look in the Tutorials section for Database Programming. Super Search for things such as DBI, DBIx, MySQL, SQLite, DBM::Deep and so on. It's a hot topic around here.

    DWIM is Perl's answer to Gödel
Re: MySQL and perl? Or something else?
by jonadab (Parson) on Mar 14, 2007 at 20:43 UTC
    They suggested writing an address book and accessing it, and I realized that this could be a great way to learn about hashes.

    You could do this with hashes, Data::Dumper, and do, or you could do it with DBI. If you want to learn to do database stuff, DBI is good way to go.

    Actually it sounds like a reasonably good excuse for learning Class::DBI. For the backend, yeah, MySQL (with DBD::mysql) is fine for your requirements or you could even use DBD::SQLite. I assume you know how to install modules from the CPAN?

    We're working on a six-year set of freely redistributable Vacation Bible School materials.
Re: MySQL and perl? Or something else?
by Rhandom (Curate) on Mar 14, 2007 at 20:45 UTC
    There are numerous frameworks that will help you do this sort of thing. Many of them are a little over the top. One that may fit the job (without getting in your way too much) is CGI::Ex::App which is part of the CGI::Ex suite. The perldoc for CGI::Ex::App has a recipe book example that is fairly basic.

    The sample from the perldoc normally puts all of the html in separate files but CGI::Ex::App allows you to rapid prototype by storing the html in the same file as your script using subs called my_step_file_print which is what I have done below.

    Just copy the code below into a file in a cgi directory (or mod_perl registry). Change the datebase connection settings returned by the db_conn method. Chmod it 755. Then go to it in a browser. It has all of the parts of a CRUD (create read update delete) interface for the simple recipe records.

    The long debug that prints out after the html shows what methods the CGI::Ex::App used to run each step and can be removed by commenting out the post_navigate method.

    For more information as to what is going on, you should read the CGI::Ex::App perldoc.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re: MySQL and perl? Or something else?
by cormanaz (Chaplain) on Mar 14, 2007 at 22:52 UTC
    I'm confused about what you're trying to implement. A way to learn about hashes? A GUI debugger like the one in Komodo would be better for that. A way to store your notes? You could do that with MySQL.

    MySQL is a relational database, and you can implement address books (and other data storage schemes) with it and access its dbs with Perl. Once you know how to use MySQL there are Perl modules mentioned in the other replies you can use to talk to it.

    IMO the Perl side is pretty straightforward--you connect to the DB, issue a MySQL command, read the reply into an array or something and use it just like any other data in Perl.

    The learning curve is more on the MySQL side, and you should learn that before you try to run it with Perl.

Re: MySQL and perl? Or something else?
by radiantmatrix (Parson) on Mar 15, 2007 at 14:23 UTC

    A database of sorts does seem to be the way to go, but the concept of "database" doesn't automatically imply something as robust as MySQL (which belongs to a class of databases called RDBMS -- Relational DataBase Management Systems).

    It looks like what you're interested in is a category of problem called "data serialization" -- storing data structures (usually to disk) for later retrieval. There are plenty of modules to do this, including Storable and DBM::Deep.

    Considering that you're trying to learn more about data structures, though, I might suggest you look at one of the human-readable serialization formats, like YAML.

    You can serialize data to database systems (even RDBMS) as well, but I'd tackle that separately from learning about managing data structures -- no sense piling your plate too high (do the simplest thing that could possibly work!). When you're ready, you'll want to grab a book or two on Relational theory so that you can design good uses for RDBMS. Then, you'll want to check out the DBI module.

    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: MySQL and perl? Or something else?
by hangon (Deacon) on Mar 15, 2007 at 12:07 UTC

    You're probably too focused in on hashes initially. The best way to start is to do some research on data structures, then decide how to structure your address book in a more abstract manner. Once this is done it would be easier to see how you could use hashes to implement this project.

    An address book actually is a good project for learning how to structure data, since you can start with a basic flat list and evolve it into an arbitrarily complex contact management system. This could give you the opportunity to play with a variety of data structures.

Re: MySQL and perl? Or something else?
by bart (Canon) on Mar 15, 2007 at 11:53 UTC
    but with the hypothetical user constantly adding new fields within new fields within new fields, ...
    Duh? How the heck are you organizing your data structures? I can see no reason to indefinitely nest fields. But maybe that's just me.

    Can you expand on how you see these things? (Newbies often have a refreshing new look on old problems, unhindered by tradition, so that's why I'm curious.)

      It would be indefinite because the user would have the ability to create new fields of information, and new fields could beget new fields.

      i.e. One person has multiple web addresses, the user could categorize them under "web addresses," a field they add in, under which could be sub-categories "work," "personal," etc.
      C(qw/74 97 104 112/);sub C{while(@_){$c**=$C;print (map{chr($C!=$c?shift:pop)}$_),$C+=@_%2!=1?1:0}}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://604877]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-02-20 02:57 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (293 votes). Check out past polls.