Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: The dreaded if-elsif-else construct (code)

by toma (Vicar)
on Nov 18, 2001 at 01:42 UTC ( [id://126044]=note: print w/replies, xml ) Need Help??


in reply to The dreaded if-elsif-else construct (code)

I recommend converting your directory structure to an XML file. Then you can apply any one of a number of excellent XML perl modules to the problem.

A hierarchical data structure is a natural for an XML document. For your case it could look like this:

<data> <sprocket_logs> <spacely>200</spacely> </sprocket_logs> <cog_logs> <cogswell>100</cogswell> <astro>1</astro> <maimed_a_robot /> </cog_logs> </data>
Producing this file should be relatively easy using File::Find to load up a data structure and one of the XML modules for dumping the data structure.

Then you have many choices for eliminating your big switch statement. Here are a few ideas that came to mind; there are better ideas, also:

  • Access your XML as if it were a database using DBD::AnyData.

  • Create a multilevel hash from the XML with XML::Simple or one of the SAX modules. Make a hash of subs that perform each action that you would have in your giant switch statement. Dispatch these subs as you loop through the multilevel hash.
  • Use XSLT to replace the perl code that generates the existing HTML view of your directory structure.
The XML approach will be easier to maintain because the appropriate XML tools are in wide usage and worth learning anyway.

It should work perfectly the first time! - toma

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found