Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Building a hash of employees.

by DigitalKitty (Parson)
on Jul 02, 2003 at 06:12 UTC ( [id://270713]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all.

This small program is tentatively planned for inclusion into a much larger design. However, I do feel as though I could benefit from any advice you are willing to impart.

My employer would like a perl based app. that will store the names, phone numbers, etc. of his current staff. My fledging effort is below:

#!/usr/bin/perl -w use strict; my %staff = ( Mike => { age => 27, skills => "python, java, bioinformatics, biochemistry", phone => '213.xxx.xxxx', email => 'xxx@xxx.xxx', }, Cindy => { age => 23, skills => "perl, java, bioinformatics, microbiology", phone => '213.xxx.xxxx', email => 'xxx@xxx.xxx', }, Katie => { age => 24, skills => "perl, java, C, XML, " . "MySQL, PHP, " . "bioinformatics, genetic programming,\n" . "chemical analysis", phone => '310.xxx.xxxx', email => 'xxx@xxx.xxx', }, Carl => { age => 25, skills => "Python, PHP, postgresql, bioinformatics, biochemistry" +, phone => '310.xxx.xxxx', email => 'xxx@xxx.xxx', }, Michelle => { age => 31, skills => "bioinformatics, biochemistry, molecular biology", phone => '415.xxx.xxxx', email => 'xxx@xxx.xxx', }, );


Thanks,
-Katie.

Replies are listed 'Best First'.
Re: Building a hash of employees.
by artist (Parson) on Jul 02, 2003 at 06:55 UTC
    Storing data in XML could be a good option as well. That way the future transition/expansion would be easy. The file can be maintained by non-perl people as well. Each person for example, can maintain his/her own profile in XML (given DTD) and you can integrate that data for your application.

    Your Hash structure is fine. I would extend it a further bit and put all the 'names' in the array, thus you don't accidentally run into problems when there are 2 Mike.

    $employee = { name => 'Mike Smith' , age => 31 , ...}; push @employees, $employee;

    artist

      I agree that some sort of serialisation would help, but not necessarily XML. Others have suggested DBM or SQL databases. Personally, I find Storable quick and easy for this type of thing. If you want to use SQL, you might want to consider SQLite which I've used recently for several lightweight database applications where I didn't want a fully featured relational database server.

      I like XML and use it where appropriate, but sometimes developers seem to use XML because they think everyone else does it, or because it's fashionable at the moment. If it works for you as a serialisation method, fine, use it, but make sure you consider the alternatives.

      I disagree with the idea that each person can maintain his/her own profile in XML, as I would want to enforce strong data validation. You can't write a DTD to check that someone enters their age as a number, or that you can't have a birthday on June 31st. To do this, you need to check imput data using something like Params::Validate, perhaps with Regexp::Common. If you go down the XML route for this, you have to use something complicated like XML Schema.

      If you want to migrate your data to a different programming environment, you can easily convert your data to XML at that point using one of Perl's many XML modules such as XML::Simple.

Re: Building a hash of employees.
by archon (Monk) on Jul 02, 2003 at 06:25 UTC

    er.. what's your question? if the size of the staff is to remain that small, the hash is probably fine. you could tie() it to a *DBM_File if you wanted to.

    for larger data sets, a real database table might be optimal. your current format would make it easy to insert.

Re: Building a hash of employees.
by djantzen (Priest) on Jul 02, 2003 at 06:30 UTC

    Since this is a prototype you're probably fine using a simple hash, but archon's suggestion to use a DBM or database is good advice if the project begins to expand. I think I'd be inclined to keep the variable length entries (i.e., skills) in an array though. It'll be cleaner to iterate over an array than to ensure properly formatted strings to split on commas upon retrieval.


    "The dead do not recognize context" -- Kai, Lexx
Re: Building a hash of employees.
by Skeeve (Parson) on Jul 02, 2003 at 10:39 UTC
    Did it come to your mind, that male people get older?

    So it's fine and perfectly okay to write:

    : Katie => { age => 24, : :
    But you should consider for male employees:
    : Carl => { born => '1978-04-01', : :
    SCNR ;-)
Re: Building a hash of employees.
by rdfield (Priest) on Jul 02, 2003 at 09:30 UTC
    MDBLM (or any other tied hash) is what you need for persistence when you're only searching via the firstname (how long before you'll have to come up with a scheme to store multiple occurances of the same first name?), but what happens when you're looking for people with a particular skill or in a particular telephone area code? To future-proof this structure you'll need an SQL database.

    rdfield

Re: Building a hash of employees.
by Mr. Muskrat (Canon) on Jul 02, 2003 at 13:58 UTC
    How about making the skills keys an anonymous array?
    Katie => { age => 24, skills => [ 'perl', 'java', 'C', 'XML', 'MySQL', 'PHP', 'bioinformatics', 'genetic programming', 'chemical analysis', ], phone => '310.xxx.xxxx', email => 'xxx@xxx.xxx', },
Re: Building a hash of employees.
by NetWallah (Canon) on Jul 02, 2003 at 16:33 UTC
    I would consider it a very poor practice, to hard-code data into a program.

    As has been suggested before, saving the data in XML would buy you a whole lot of data independence, extensibility, and functionality - you get built-in Xpath, searches, and data validation using XML modules.

    You may encounter resistance based on ignorance, but I encourage you to learn XML if necessary - there is no down-side. Here is a sample based on your requirements:

    <?xml version='1.0'?> <AllEmployees> <emp name="Mike" age = '27' phone = '213.xxx.xxxx' email = 'xxx@xxx.xxx'> <skills> <computerlanguage name= "python" skilllevel='5'> <computerlanguage name= "java" skilllevel='2'> <computerlanguage name= "python" skilllevel='5'> <speciality name='bioinformatics'> <speciality name='biochemistry'> </skills> </emp> <emp name='Cindy' age='23' ....etc <skills> ...etc... </skills> </emp> ...etc </AllEmployees>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://270713]
Approved by allolex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found