waxmop has asked for the wisdom of the Perl Monks concerning the following question:
Greetings wise ones:
I just wrote a clunky application, and I want to get the community's feedback, because I don't like how I wrote it.
Background: the people I work for need a way to search, add, and edit their records of contacts at a bunch of different companies. For example, when somebody gets a new phone number, we need to be able to update our records.
Here's the overview of what I did:
- Turned the excel spreadsheet that held all the orders into an xml file.
- Wrote a search.html page that gets information from a form, and then uses perl-mason and XML::Simple to find matching records and print them.
- For any printed record, a new form appears that allows the user to fill out updated information. Then, the perl-mason code replaces the old record with the new one. This is the part that seems clunky to me.
The xml file looks sort of like this:
I wrote some perl-mason pages to allow users to search records after parsing the data with XML::Simple. They can edit a record by filling out a form on the post_edits.html page. Here's what happens on the post_edits.html page:<records> <record> <uid>0001</uid> <company_name>Acme Industries</company_name> <contact_name>Arthur Dent</contact_name> <contact_phone_number>867-5309</contact_phone_number> </record> <record> <uid>0002</uid> <company_name>Zeta Industries</company_name> <contact_name>Sam Lowry</contact_name> <contact_phone_number>555-5555</contact_phone_number> </record> </records>
<%init> use XML::Simple; my $xref = XMLin("path_to_xmlfile.xml"); my $newnode = {}; foreach my $kee ( "uid", "company_name", "contact_name", "contact_phone_number" ) { $newnode->{$kee} = $ARGS{$kee}; } foreach my $rec ( @{$xref->{record}} ) { if ( $rec->{'uid'} eq $newnode->{'uid'} ) { $rec = $newnode; } } </%init>
I pass the new edited record in the %ARGS hash. Then I create the $newnode structure which will replace the old record. Then I loop through my in-memory structure until I find the record that I want to replace, and then I replace it, with the $rec = $newnode line. I've used Data::Dumper to check that it works, and it looks good, but I can't help but wonder if there is a better way.
All comments and criticism are welcome. Let the didactic abuse flow!
update (broquaint): added missing </ul>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: best way to change xml record using XML::Simple?
by RiotTown (Scribe) on Feb 24, 2003 at 18:48 UTC | |
by waxmop (Beadle) on Feb 24, 2003 at 18:53 UTC | |
by merlyn (Sage) on Feb 24, 2003 at 19:13 UTC | |
Re: best way to change xml record using XML::Simple?
by CountZero (Bishop) on Feb 24, 2003 at 19:40 UTC | |
by seattlejohn (Deacon) on Feb 24, 2003 at 20:33 UTC | |
by CountZero (Bishop) on Feb 24, 2003 at 21:41 UTC | |
by waxmop (Beadle) on Feb 24, 2003 at 21:26 UTC | |
by seattlejohn (Deacon) on Feb 24, 2003 at 23:18 UTC | |
Re: best way to change xml record using XML::Simple?
by seattlejohn (Deacon) on Feb 24, 2003 at 20:21 UTC | |
by waxmop (Beadle) on Feb 24, 2003 at 20:49 UTC | |
Re: best way to change xml record using XML::Simple?
by BrowserUk (Patriarch) on Feb 24, 2003 at 21:24 UTC | |
by waxmop (Beadle) on Feb 24, 2003 at 21:55 UTC | |
by Zaxo (Archbishop) on Feb 24, 2003 at 22:07 UTC | |
by BrowserUk (Patriarch) on Feb 24, 2003 at 22:13 UTC | |
by pfaut (Priest) on Feb 24, 2003 at 22:34 UTC |