http://qs321.pair.com?node_id=238192

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:

The xml file looks sort of like this:

<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>
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:
<%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>